Skip to content

Switch typeguard for single case #59953

Closed as not planned
Closed as not planned
@aaaaaa-aaaaa

Description

@aaaaaa-aaaaa

🔎 Search Terms

"type guard typescript not working for single case switch"

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______
  • I tested this in typescript playground for versions 5.6.2, 4.6.4, and 3.3.3

⏯ Playground Link

https://www.typescriptlang.org/pt/play/?#code/C4TwDgpgBAYg9nKBeKBvAUASFJAXFAcgEMCAaLI-AZ2ACcBLAOwHNyBfddAMwFdGBjYPTiMoXBAApxcfPDgBKNFioB3esH4ALKFIQA6HBEUZMmfkSrRiBXFlP8RVOABsIe53Ga64eovLuYtBDAPLSMANycpgAmEFxEPM7AtqZmjsBQAPoQAB6aCTT0AG4QAMKaEPwA1viMECW0yGIIkZgcHOiGUABCRI0oJob41uSYVDwARkOEJKOUUDQMLOxQAD5K2OAQw7PKk9MEE2RYE9R0TKzoHbwCQiJQE30Sj7T4vbTGymoa2s99BltPvYLFYSCl7I4XG4PF5rFAmA8+lAAPyI2i+KD4F56Cb+VJBEJhSJ2WLxRLJAIORg0LK5fI8QolcqVGpQOoNJovVrtIA

💻 Code

type Foo = {
	type: 'a',
	a: string,
}

function foo(foo: Foo) {
	switch (foo.type) {
		case 'a':
			console.log(foo.a)
			return;

		default:
			const _exhaustiveCheck: never = foo;
	}
}

type Bar = {
	type: 'a',
	subtype: 'a',
	a: string,
} | {
	type: 'a',
	subtype: 'b',
	b: string,
}

function bar(bar: Bar) {
	switch (bar.type) {
		case 'a':
			console.log('a' in bar ? bar.a : bar.b)
			return;

		default:
			const _exhaustiveCheck: never = bar;
	}
}

🙁 Actual behavior

No type reduction occurs in the default case. Typescript marks foo not assignable to type never.

🙂 Expected behavior

Both foo and bar should be reduced to type never in the default case.

Additional information about the issue

Related: #2214

Workaround:

Manually exclude types from foo that are handled by case 'a'.

type Foo = {
	type: 'a',
	a: string,
}

function foo(foo: Foo) {
	switch (foo.type) {
		case 'a':
			console.log(foo.a)
			return;

		default:
			const _exhaustiveCheck: never = foo as Exclude<typeof foo, { type: 'a' }>;
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions