-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Description
TypeScript Version: nightly (on playground).
Search Terms:
generic, return, assertion, inference, checker api.
Code
declare function genericGet<TValue>(object: any, path: string): TValue
const object = { foo: { bar: 123 } };
const result1 = genericGet(object, 'foo.bar');
const result2 = genericGet(object, 'foo.bar') as number;
Expected behavior:
In both cases, the return type of genericGet(object, 'foo.bar')
reported by the type checker API (checker.getTypeAtLocation
) should be unknown
.
Actual behavior:
In the first case, the return type of genericGet(object, 'foo.bar')
reported by the type checker API is unknown
.
In the second case, the return type is number
.
(best shown via ts-ast-viewer, link below).
Related Issues:
#31435
Loosely related to: #35431
Related issue in typescript-eslint:
typescript-eslint/typescript-eslint#1410
I found #31435, which seems to suggest that this is entirely intentional.
However I would like to question this from the context of the type checker API.
This makes the type API pretty hard to use for certain use cases. Inspecting the type of the assertion expression (via checker.getTypeAtLocation
) says it's of type number, and similarly inspecting the type of the call expression via the same API says it's of type number.
Is there any way to get the type that hasn't been inferred from a return type assertion?
In the context of our no-unnecessary-type-assertion
rule, the rule warns about assertions that don't do anything, so that there is less useless code in a codebase..
(i.e. genericGet(object, 'foo.bar') as number
is necessary, because it asserts unknown
to number
, but genericGet<number>(object, 'foo.bar') as number
is unnecessary, because the return type is already number
).