Skip to content

Unknown types narrowed to a union won't narrow further #30557

Closed
@Validark

Description

@Validark

This fails on the playground, and I believe it also fails on [email protected]

TypeScript Version: [email protected]

Search Terms: unknown intersection failure types narrow

Code

interface TypeA {
	Name: "TypeA";
	Value1: "Cool stuff!";
}

interface TypeB {
	Name: "TypeB";
	Value2: 0;
}

type Type = TypeA | TypeB;

declare function isType(x: unknown): x is Type;

function WorksProperly(data: Type) {
	if (data.Name === "TypeA") {
		// data: TypeA
		const value1 = data.Value1;
	}
}

function DoesNotWork(data: unknown) {
	if (isType(data)) {
		if (data.Name === "TypeA") {
			// data: Type
			// data should be TypeA
			const value1 = data.Value1; // type error!
		}
	}
}

Expected behavior: in DoesNotWork, data should be TypeA after we can guarentee it is named "TypeA"

Actual behavior: data is still only a Type

Playground Link: Here

Related Issues: I looked through several pages of issues and did not find anything similar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions