Skip to content

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

Open
@JakeTunaley

Description

@JakeTunaley

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

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @RyanCavanaugh@JakeTunaley

        Issue actions

          Indexed this types don't narrow when their referenced property narrows · Issue #26759 · microsoft/TypeScript