Open
Description
TypeScript Version: 3.1.0-dev.20180829
Search Terms: this index narrowing generics
Code
type Keys = 'string' | 'number';
type Switch<T extends Keys> =
T extends 'string' ? string :
T extends 'number' ? number :
never;
interface Main<T extends Keys> {
kind: T;
value: Switch<this['kind']>;
}
let x: Main<Keys> = null as any;
x.kind;
x.value;
if (x.kind === 'string') {
x.kind; // Narrowed to "string"
x.value;
} else {
x.kind; // Narrowed to "number"
x.value;
}
Expected behavior:
In the first if block, x.value
should be of type string
. In the second if block, x.value
should be of type number
.
Actual behavior:
In both cases x.value
is not narrowed, despite x.kind
narrowing successfully.
Related Issues: #24085
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity