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

Switch typeguard for single case #59953

Closed
aaaaaa-aaaaa opened this issue Sep 13, 2024 · 2 comments
Closed

Switch typeguard for single case #59953

aaaaaa-aaaaa opened this issue Sep 13, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@aaaaaa-aaaaa
Copy link

aaaaaa-aaaaa commented Sep 13, 2024

🔎 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' }>;
	}
}
@MartinJohns
Copy link
Contributor

Duplicate of #18056. A bit of time has passed since then, maybe things changed and it's not too complex anymore.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 13, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants