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

Contextual type isn't provided right when generic type discriminator exists in two properties #57548

Closed
osasson opened this issue Feb 26, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@osasson
Copy link

osasson commented Feb 26, 2024

πŸ”Ž Search Terms

"generic type discriminator"

πŸ•— Version & Regression Information

Version 5.2.2

⏯ Playground Link

No response

πŸ’» Code

type A = {
	1: { dt: string },
	2: { num: number },
};
type B = { [K in keyof A]: {
	extra: { id: number, reason: K }
	data: A[K]
 } }[keyof A];

 type C = { [K in keyof A]: {
	data: A[K] & { reason: K }
 } }[keyof A];

const test:B = {} as any;
if (test.extra.reason === 2) {
	// Property 'num' does not exist on type '{ dt: string; } | { num: number; }'. Property 'num' does not exist on type '{ dt: string; }'.ts(2339)
	console.log(test.data.num);
}
// expected no error
const test1:C = {} as any;
if (test1.data.reason == 2) {
	console.log(test1.data.num);
}

// expected error
const test2:B = {
	extra: { id: 1, reason: 2 },
	data: { dt: 'string' },
};

// expected no error
const test3:B = {
	extra: { id: 1, reason: 2 },
	data: { num: 123 },
};

πŸ™ Actual behavior

Typescript does not discriminate the type according to its constraints

πŸ™‚ Expected behavior

discrimination should occur

Additional information about the issue

No response

@RyanCavanaugh
Copy link
Member

Duplicate #18758

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 26, 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 Feb 29, 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

3 participants