Skip to content

Type not narrowed by checking the value of a template literal type #53887

Description

Bug Report

🔎 Search Terms

template literal, string interpolation, type narrowing

🕗 Version & Regression Information

  • I was unable to test this on prior versions prior to 4.1 because template literal types were only added then

⏯ Playground Link

I have a hard time describing this issue, the playground link will explain it better I think:

Playground Link

I would expect fn2 to work in the same way that fn1 does

💻 Code

type A = {
	type: "atype";
	acontent: string;
}

type B = {
	type: "btype";
	bcontent: string
}

function fn1(val: A|B) {
	if(val.type === "atype") {
		console.log(val.acontent);
	}
	if(val.type === "btype") {
		console.log(val.bcontent);
	}
}

type Modified<T extends A|B> =
	Omit<T, "type"> & {
		type: `prefix-${T["type"]}`;
	};

function fn2(val: Modified<A|B>) {
	if(val.type === "prefix-atype") {
		console.log(val.acontent);
	}
	if(val.type === "prefix-btype") {
		console.log(val.bcontent);
	}
}

🙁 Actual behavior

Errors in the console.logs in fn2

🙂 Expected behavior

No errors, same as in fn1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions