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

Conditional types, overloads, and declaration files. #35657

Closed
MicahZoltu opened this issue Dec 12, 2019 · 3 comments
Closed

Conditional types, overloads, and declaration files. #35657

MicahZoltu opened this issue Dec 12, 2019 · 3 comments

Comments

@MicahZoltu
Copy link
Contributor

TypeScript Version: 3.6.3

Search Terms:
conditional type overload declaration file

Code
Typescript file:

export class Apple {
	constructor(a: string)
	constructor(a: number)
	constructor(private readonly a: string|number) {}
}

Generated declaration file:

export class Apple {
	constructor(a: string);
	constructor(a: number);
}

Usage:

type AppleArgs = ConstructorParameters<typeof Apple>

Expected behavior:
Whether the usage is inside the same package (.ts file referenced) or another package (.d.ts file referenced) AppleArgs should be [string|number].

Actual behavior:
If the usage is in the same package, AppleArgs is [string|number].
If the usage is in a different package, AppleArgs is [number].

Playground Link: https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=6&pc=1#code/CYUwxgNghgTiAEkoGdnwIIAdMQQbwCh5jEB7AO2QBcYBXMK0mACigC55qYBLcgcwCUREmApd6jFu3jlaAWwBGIGEIC+BKgE9MCLDhDoYfNAF54AYTE0JTAAqwockFWXIAPFp2kAZhmy4APgIgA
Not a great playground link since this is an issue with declaration emission.

Related Issues: I understand that conditional types (like ConstructorParameters) use the last overload. What I believe is a "bug" here is that the emitted declaration file results in different behavior when the class is referenced from another package vs when it is referenced from the same package. My expectation is that the behavior will be the same in either case.

@fatcerberus
Copy link

Like you, I'm most surprised that the behavior differs - the implementation signature is (by design) not considered one of the overloads, so if what you've put under "generated declaration file" is accurate, ConstructorParameters<typeof Apple> should be the same type regardless of whether it occurs inside or outside of the implementing package.

@MicahZoltu
Copy link
Contributor Author

For the sake of conditional types, I believe that the implementation signature is considered when it comes to "picking the last overload". This is different from other parts of tsc where the implementation signature isn't considered.

@MicahZoltu
Copy link
Contributor Author

Hmm, I just realized that I cannot reproduce the desired/expected behavior in Playground, so my issue may be something else. I'm going to close this for now since I don't have a clear repro case that exhibits different behavior between .d.ts and .ts, though the current behavior is frustrating and confusing, I think it is "by design".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants