Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it intentional that function signatures don't include generic constraints? #2226

Closed
RunDevelopment opened this issue Apr 9, 2023 · 4 comments
Labels
enhancement Improved functionality
Milestone

Comments

@RunDevelopment
Copy link
Contributor

Search terms

function signature, generic constraints, extends

Question

I recently updated to 0.24.1 and noticed this:
image

I only read the function signature at first and wondered how T["parent"] is valid TS. After all, T could be any type, so there might not be a parent field. Only after looking at the Type Parameters section did I see the constraint.

So I wanted to ask whether this was intentional.

I can see the appeal of saving precious screen space, but generic constraints can be necessary for the signature to make sense.

@RunDevelopment RunDevelopment added the question Question about functionality label Apr 9, 2023
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Apr 9, 2023

This is intentional, I'm not sure how much I agree with it though, it was a decision made before I started maintaining typedoc... could argue that if that type is omitted, shouldn't parameters be as well?

@RunDevelopment
Copy link
Contributor Author

I just thought that this might be a problem for function overloads. What if 2 functions overloads most meaningfully differ in constraints of their type parameters? Example:

function foo<T extends string>(arg: T): `${T}!`;
function foo<T extends number>(arg: T): T;
function foo(arg: string | number): string | number {
	if (typeof arg === "number") {
		return arg
	}
	return arg + "!"
}

const a = foo("abc") // type: "abc!"
const b = foo(42)    // type: 42

If typedoc only displays the following, then it'll be quite confusing to readers.

foo<T>(arg: T): `${T}!`
foo<T>(arg: T): T

Of course, those are edge cases. I don't know whether making them unambiguous is worth the screen space. The type of constraints might be quite long, and signatures can get very long even without them.

@Gerrit0 Gerrit0 added enhancement Improved functionality and removed question Question about functionality labels Apr 16, 2023
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Apr 16, 2023

I should have made --hideParameterTypesInTitle also change this, repurposing this issue to make that change

@RunDevelopment
Copy link
Contributor Author

Thank you @Gerrit0!

@Gerrit0 Gerrit0 added this to the v0.24.5 milestone Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improved functionality
Projects
None yet
Development

No branches or pull requests

2 participants