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
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
declareconstfoo: Object;declarefunctionf<V=any,R=any>(p: {[key: string]: V}): V|R;f(foo);// mouse over f, its type is f<any,any>f(foo)asstring;// 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.
The text was updated successfully, but these errors were encountered:
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
Expected behavior:
No errors.
Actual behavior:
It seems like the
as string
type assertion changes the type inference ofV
andR
, which is quite surprising. Is this a regression or intended?Playground Link:
https://www.typescriptlang.org/play/#src=declare%20const%20foo%3A%20Object%3B%0D%0Adeclare%20function%20f%3CV%20%3D%20any%2C%20R%20%3D%20any%3E(p%3A%20%7B%5Bkey%3A%20string%5D%3A%20V%7D)%3A%20V%7CR%3B%0D%0Af(foo)%3B%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20f%20is%20f%3Cany%2Cany%3E%0D%0Af(foo)%20as%20string%3B%20%20%2F%2F%20f%20is%20f%3Cstring%2Cstring%3E
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 typeany
but what do I know.The text was updated successfully, but these errors were encountered: