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

Type discrimination bug with string literal tuple. #59785

Closed
gyurielf opened this issue Aug 28, 2024 · 4 comments
Closed

Type discrimination bug with string literal tuple. #59785

gyurielf opened this issue Aug 28, 2024 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@gyurielf
Copy link

gyurielf commented Aug 28, 2024

πŸ”Ž Search Terms

"discriminated union", "discrimination", "literal tuple", "string literal tuple"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAQghgZ2gXigbwLACgq6gSwBMB+ALigWACd8A7AcwG5s8oBrCEMi6up7AL7ZsoSFACCAFXAp0LPHCpU4IcgG0A5HA0BdZjjwAzfBAA2hBNzRRacALYRulGgwA0UOIUJUICS+Wc+AUYoAHpQqAAxE3MEDx8PU1MAewB3CEIoZNpTEChDZKooLQ1BfREZWGkxVEwDXEVlVShNACNdfVYfQnwfAGNgAFUAJQAZchHRqAAfHhcmMIjhiABHAFdejPzC4vay4SxRaHE4WkIYatk61kaVdRL3DXadGZanjUftPXlcbs2ByYTMavQIMELhKDLdabTIFIrvH5QQgQQxwNamYAASWAEDs3BKr3enSMMQsVhs9kcAV4bg8Xh8ficNPowUWUVJcUU0DgSTSW1SAAsILQoK1ksABcVtB4zrsNPFoGAGcLgPssBUxABhbLGehQVDwJBQABkUAAFFJKrMLlaJKdzpcAJTlLB9bKUKA4yjkbW0XX6uRYACQt2ami+rmwQb+-SGY3IGgFwGAYAQpHCEAAHvYwKYIAA6N12eXhAA85YAtFXPQL8HEEALkujMhKqGkZVAIEpCvnBEA

πŸ’» Code

type Base = {
    id?: string;
    key?: string;
}

type AType = {
    array: ['a'];
    fields?: { name?: string, address?: string}; // Fields are allowed only for 'a'
};

type BType = {
    array: ['b'];
    redirectURL: URL | string; // Required for 'b'
};

type AandBType = {
    array: ['a', 'b'] | ['b', 'a'];
    redirectURL: URL | string; // Required for 'b'
    defaultItem?: 'a' | 'b';
    fields?: { name?: string, address?: string}; // Fields are allowed when both 'a' and 'b' are present
};

type Config = Base & (AType | BType | AandBType);

const test: Config = {
	array: ['a'],
	redirectURL: 'https://example.com' //<<<--- this should throw an error.
}

πŸ™ Actual behavior

The following test variable is suggest every field regardless of the array: ['a'].

const test: Config = {
	array: ['a']
}

And it not thrown errors if i declare a non related property.

const test: Config = {
	array: ['a'],
	redirectURL: 'https://example.com', //<<<--- this should throw an error.
	defaultItem: 'a' //<<<--- this should throw an error as well.
}

πŸ™‚ Expected behavior

The following test variable should suggest the discriminated type fields by default.

const test: Config = {
	array: ['a']
}

And it not thrown errors if i declare a non related property.

const test: Config = {
	array: ['a'],
	redirectURL: 'https://example.com', //<<<--- this should throw an error.
	defaultItem: 'a' //<<<--- this should throw an error as well.
}

Additional information about the issue

No response

@gyurielf gyurielf changed the title Type discrimination with array of string literal. Type discrimination with string literal touple. Aug 28, 2024
@gyurielf gyurielf changed the title Type discrimination with string literal touple. Type discrimination bug with string literal touple. Aug 28, 2024
@RobertSandiford
Copy link

Config can be rewritten as type Config = (Base & AType) | (Base & BType) | (Base & AandBType) with the same result.
I looked at this and figured that when checking compatibility the checker could look for matching union members, then apply the Excess Property Check to each matching member, ruling out any that fail, or throw an EPC error if they all fail. But it doesn't seem to.

Changing array to a string, which TS seems to need to use it as a discriminator makes the EPC kick in. But I'm just not getting why an EPC check on a union needs a discriminator. I may be missing something, but seemed solvable to me.

@MartinJohns
Copy link
Contributor

But I'm just not getting why an EPC check on a union needs a discriminator.

#20863

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 28, 2024
@jcalz

This comment was marked as resolved.

@gyurielf gyurielf changed the title Type discrimination bug with string literal touple. Type discrimination bug with string literal tuple. Aug 29, 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 1, 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

6 participants