-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Type Inference does not work for generics with default types #19205
Comments
Currently generic type inference happens only when no generic type arguments are provided. specifying at least one disables all generic inference. |
closing in favor of #10571 |
Thanks for the clarification, indeed the following example works: function test<T, R>(param: FormattedDataProvider<T, R>) {
console.log(param);
}
test({
src: { a: 23 },
format: v => String(v.a)
}) This one however, does not. So even when not specifying any types, the defaults might disable inference, too, in some instances. type FormattedDataProvider<T, R = {}> = {
src: R,
format: (item: R) => T
}
type UnformattedDataProvider<T> = {
src: T
}
type ConfiguredDataProvider<T> = FormattedDataProvider<T> | UnformattedDataProvider<T>;
function test<T>(param: ConfiguredDataProvider<T>) {
console.log(param);
}
test({
src: { a: 23 },
format: v => String(v.a)
}) |
in the above example, you have used if you wanted to propagate the optionality of the second argument you would write it as: type FormattedDataProvider<T, R = {}> = {
src: R,
format: (item: R) => T
}
type UnformattedDataProvider<T> = {
src: T
}
type ConfiguredDataProvider<T, R = {}> = FormattedDataProvider<T, R> | UnformattedDataProvider<T>;
function test<T, R = {}>(param: ConfiguredDataProvider<T, R>) {
console.log(param);
} |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.6.0-dev.201xxxxx
Code
Expected behavior:
This should compile and infer that v is of type
{a: number}
.Actual behavior:
v is inferred to the default type {}, which results in an error:
error TS2339: Property 'a' does not exist on type '{}'.
The text was updated successfully, but these errors were encountered: