You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type discrimination bug with string literal tuple.Β #59785
typeBase={id?: string;key?: string;}typeAType={array: ['a'];fields?: {name?: string,address?: string};// Fields are allowed only for 'a'};typeBType={array: ['b'];redirectURL: URL|string;// Required for 'b'};typeAandBType={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};typeConfig=Base&(AType|BType|AandBType);consttest: 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'].
consttest: Config={array: ['a']}
And it not thrown errors if i declare a non related property.
consttest: 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.
consttest: Config={array: ['a']}
And it not thrown errors if i declare a non related property.
consttest: Config={array: ['a'],redirectURL: 'https://example.com',//<<<--- this should throw an error.defaultItem: 'a'//<<<--- this should throw an error as well.}
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.
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
Configcan 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
arrayto 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.