You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms:
generic inferring destructing function parameter
Description
I declared generic function with two generic types. One is depended on the other by keyof. Function takes one argument that is destructing and described by one interface (also generic). One of the parameters should be key of other one.
When I pass object with only these two depended properties TS throw confusing error.
After passing all properties to that object TS not complainig about this any more.
Moreover, if I change function to take seperate arguments (with the same typings) there is no such error.
Expected behavior:
Should throw error telling me that passed parameter is not assignable to interface ListProps, because it don't implement third property.
Moreover, TS is not inferring types of first two properties - I think it should.
Actual behavior:
There is confusing error after passing two of three properties with object as function argument:
Type 'string' is not assignable to type 'never'.(2322)
input.tsx(3, 3): The expected type comes from property 'itemKey' which is declared here on type 'ListProps<unknown, never>'
Related Issues:
Code
exportinterfaceListProps<T,KextendskeyofT>{items: T[];itemKey: K;prop: number}exportconstComponent=<T,KextendskeyofT>({
items,
itemKey,
prop,}: ListProps<T,K>)=>{}// throws confusing errorComponent({items: [{name:' string'}],itemKey: 'name'})// work after passing last propertyComponent({item: {name:' string'},itemKey: 'name',prop: 1})// work as expected without parameter destructingexportconstComponent2=<T,KextendskeyofT>(items: T[],itemKey: K,prop: number)=>{}// throws 'Expected 3 arguments, but got 2.'Component2([{name:' string'}],'name')
You're right, there's a little quirk in type inference that causes us to sometimes not infer types for T and K in the case where a property is missing in the source. We ought to make inferences and then report a missing property error, as you suggest.
TypeScript Version: 4.0.5
Search Terms:
generic inferring destructing function parameter
Description
I declared generic function with two generic types. One is depended on the other by
keyof
. Function takes one argument that is destructing and described by one interface (also generic). One of the parameters should be key of other one.When I pass object with only these two depended properties TS throw confusing error.
After passing all properties to that object TS not complainig about this any more.
Moreover, if I change function to take seperate arguments (with the same typings) there is no such error.
Expected behavior:
Should throw error telling me that passed parameter is not assignable to interface ListProps, because it don't implement third property.
Moreover, TS is not inferring types of first two properties - I think it should.
Actual behavior:
There is confusing error after passing two of three properties with object as function argument:
Related Issues:
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: