-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
TypeScript Version: 3.1.0-dev.201xxxxx
Search Terms: nested typeguard type guard depending infer generic
Code
interface A {
b: {
a: 12;
b: 22;
}
c: {
r: 33;
}
}
function testFn<T extends keyof A>(param1: T, param2: keyof A[T]) {
if (param1 === 'b') {
// here should infer param2: 'a' | 'b' but it doesn't
assertName(param2);
}
}
function assertName(name: 'a' | 'b') {
}Expected behavior:
param2 type inferred
Actual behavior:
param2 type is string | number | symbol
Playground Link: https://www.typescriptlang.org/play/index.html#src=interface%20A%20%7B%0D%0A%20%20b%3A%20%7B%0D%0A%20%20%20%20a%3A%2012%3B%0D%0A%20%20%20%20b%3A%2022%3B%0D%0A%20%20%7D%0D%0A%20%20c%3A%20%7B%0D%0A%20%20%20%20r%3A%2033%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0Afunction%20testFn%3CT%20extends%20keyof%20A%3E(param1%3A%20T%2C%20param2%3A%20keyof%20A%5BT%5D)%20%7B%0D%0A%20%20%20%20if%20(param1%20%3D%3D%3D%20'b')%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%20here%20should%20infer%20param2%3A%20'a'%20%7C%20'b'%20but%20it%20doesn't%0D%0A%20%20%20%20%20%20%20%20assertName(param2)%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Afunction%20assertName(name%3A%20'a'%20%7C%20'b')%20%7B%0D%0A%0D%0A%7D
Related Issues: