Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexed this types don't narrow when their referenced property narrows #26759

Open
JakeTunaley opened this issue Aug 30, 2018 · 0 comments
Open
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@JakeTunaley
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants