-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
TypeScript Version: It is reproducible in 3.4.5 and 3.5.0-dev.20190515 but works in 3.3.3333.
Search Terms:
type assertion, type inference
Code
declare const foo: Object;
declare function f<V = any, R = any>(p: {[key: string]: V}): V|R;
f(foo); // mouse over f, its type is f<any,any>
f(foo) as string; // mouse over f, its type is f<string,string>Expected behavior:
No errors.
Actual behavior:
Argument of type 'Object' is not assignable to parameter of type '{ [key: string]: string; }'.
f(foo) as string; // mouse over f, its type is f<string,string>
~~~
It seems like the as string type assertion changes the type inference of V and R, which is quite surprising. Is this a regression or intended?
Related Issues:
I found #28816 (comment) which gives me a hint that TypeScript might determine the type of a type parameter in some order of preference too, similar to a parameter.
My guess is that the as string type assertion provides the contextual generic type, which is before the initializer type any but what do I know.
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug