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:
empty tuple, type inference, generic function
Code
Basically I'm writing a generic function with dynamic type mapping. I think this use case is pretty common for database drivers and JSON validators and I found some related issues. There is a workaround and I want to confirm whether it is intended behavior or a bug.
interfaceTypeMap{STRING: string;NUMBER: number;}typeMapTypes<T>={[PinkeyofT]: T[P]extendskeyofTypeMap ? TypeMap[T[P]] : never};declarefunctionfromArray<TextendsReadonlyArray<keyofTypeMap>>(types: T,params: MapTypes<T>): void;fromArray(['STRING','NUMBER','STRING'],['abc',123,'xyz']);// ok// the default behavior is to infer types in array as type unionfromArray(['STRING','NUMBER','STRING'],['abc','123','xyz']);// no errorfromArray(['STRING','NUMBER','STRING']asconst,['abc','123','xyz']);// errordeclarefunctionfromTuple<Textends([]|ReadonlyArray<keyofTypeMap>)>(types: T,params: MapTypes<T>): void;fromTuple(['STRING','NUMBER','STRING'],['abc',123,'xyz']);// ok// T extends([]|MyType) cases the compiler to infer array as tuplefromTuple(['STRING','NUMBER','STRING'],['abc','123','xyz']);// errorfromTuple(['STRING','NUMBER','STRING']asconst,['abc','123','xyz']);// error
Expected behavior:
Type of the array literal is inferred as array of union type.
Actual behavior:
Type of the array literal is inferred as tuple when T extends []|MyType is used in generic function.
I'd prefer it to be the expected behavior, but I didn't find any tests for that use case.
Can I add some tests for this behavior then? I wasn't able to find anything similar by grepping tests directory (grep 'extends \[\]' -r tests/).
There are tests that check a combination of two tuples, but not a combination of a tuple and array.
TypeScript Version: 3.4.5
Search Terms:
empty tuple, type inference, generic function
Code
Basically I'm writing a generic function with dynamic type mapping. I think this use case is pretty common for database drivers and JSON validators and I found some related issues. There is a workaround and I want to confirm whether it is intended behavior or a bug.
Expected behavior:
Type of the array literal is inferred as array of union type.
Actual behavior:
Type of the array literal is inferred as tuple when
T extends []|MyType
is used in generic function.I'd prefer it to be the expected behavior, but I didn't find any tests for that use case.
Playground Link:
You can see the same behavior in the playground
Related Issues:
#13151
#22679
Related PR
This PR includes a similar test, but it there is no test for empty tuple and array combination #26676
The text was updated successfully, but these errors were encountered: