Not planned
Description
π Search Terms
"discriminated union", "discrimination", "literal tuple", "string literal tuple"
π Version & Regression Information
- This is the behavior in every version I tried
β― Playground Link
π» 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
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
[-]Type discrimination with array of string literal.[/-][+]Type discrimination with string literal touple.[/+][-]Type discrimination with string literal touple.[/-][+]Type discrimination bug with string literal touple.[/+]RobertSandiford commentedon Aug 28, 2024
Config
can be rewritten astype 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 commentedon Aug 28, 2024
#20863
jcalz commentedon Aug 28, 2024
[-]Type discrimination bug with string literal touple.[/-][+]Type discrimination bug with string literal tuple.[/+]typescript-bot commentedon Sep 1, 2024
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.