TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Intersection causes incorrect type compatability

Open webstrand opened this issue 3 years ago • 5 comments

Bug Report

🔎 Search Terms

intersection assignability omit compatability

🕗 Version & Regression Information

  • This changed between versions 3.8.3 and 3.9.7

⏯ Playground Link

Playground link with relevant code

💻 Code

type APIApplicationCommandSubcommandGroupOption = {
    options?: APIApplicationCommandSubcommandOption[];
}

type CommandSubcommandGroupOption = Omit<APIApplicationCommandSubcommandGroupOption, 'options'> & { // <-- so is this one
  options?: CommandSubcommandOption[]
}

type CommandSubcommandOption = { otherProps: unknown } & { // <-- this intersection is apparently critical
  options?: unknown[]
}

interface APIApplicationCommandSubcommandOption {
    options?: { foo: string }[];
    otherProps: unknown;
}

function transformSubcommandGroupOption1(option: CommandSubcommandGroupOption): APIApplicationCommandSubcommandGroupOption {
  return option; // no error, but there _should_ be an error here.
}

function transformSubcommandGroupOption2(option: CommandSubcommandGroupOption): APIApplicationCommandSubcommandGroupOption {
  return { ...option }; // error in 3.9.7
}

🙁 Actual behavior

The return option does not produce an error and the return { ...option } does produce an error.

🙂 Expected behavior

Both returns should behave the same. Ideally, both should produce an error because CommandSubcommandGroupOption is incompatible with APIApplicationCommandSubcommandGroupOption.

From help thread on Discord: https://discord.com/channels/508357248330760243/964236133330460772

webstrand avatar Apr 15 '22 16:04 webstrand

Working out some bot kinks 😅

type APIApplicationCommandSubcommandGroupOption = {
    options?: APIApplicationCommandSubcommandOption[];
}

type CommandSubcommandGroupOption = Omit<APIApplicationCommandSubcommandGroupOption, 'options'> & { // <-- so is this one
  options?: CommandSubcommandOption[]
}

type CommandSubcommandOption = { otherProps: unknown } & { // <-- this intersection is apparently critical
  options?: unknown[]
}

interface APIApplicationCommandSubcommandOption {
    options?: { foo: string }[];
    otherProps: unknown;
}

function transformSubcommandGroupOption1(option: CommandSubcommandGroupOption): APIApplicationCommandSubcommandGroupOption {
  return option; // no error, but there _should_ be an error here.
}

function transformSubcommandGroupOption2(option: CommandSubcommandGroupOption): APIApplicationCommandSubcommandGroupOption {
  return { ...option }; // error in 3.9.7
}

RyanCavanaugh avatar Apr 15 '22 17:04 RyanCavanaugh

:wave: Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @webstrand

:x: Failed: -

  • Type 'CommandSubcommandGroupOption' is not assignable to type 'APIApplicationCommandSubcommandGroupOption'. Types of property 'options' are incompatible. Type 'CommandSubcommandOption[] | undefined' is not assignable to type 'APIApplicationCommandSubcommandOption[] | undefined'. Type 'CommandSubcommandOption[]' is not assignable to type 'APIApplicationCommandSubcommandOption[]'. Type 'CommandSubcommandOption' is not assignable to type 'APIApplicationCommandSubcommandOption'. Types of property 'options' are incompatible. Type 'unknown[] | undefined' is not assignable to type '{ foo: string; }[] | undefined'. Type 'unknown[]' is not assignable to type '{ foo: string; }[]'. Type 'unknown' is not assignable to type '{ foo: string; }'.
  • Type '{ options?: CommandSubcommandOption[] | undefined; }' is not assignable to type 'APIApplicationCommandSubcommandGroupOption'. Types of property 'options' are incompatible. Type 'CommandSubcommandOption[] | undefined' is not assignable to type 'APIApplicationCommandSubcommandOption[] | undefined'.
Historical Information
Version Reproduction Outputs
4.2.2, 4.3.2, 4.4.2, 4.5.2, 4.6.2

:x: Failed: -

  • Type '{ options?: CommandSubcommandOption[] | undefined; }' is not assignable to type 'APIApplicationCommandSubcommandGroupOption'. Types of property 'options' are incompatible. Type 'CommandSubcommandOption[] | undefined' is not assignable to type 'APIApplicationCommandSubcommandOption[] | undefined'. Type 'CommandSubcommandOption[]' is not assignable to type 'APIApplicationCommandSubcommandOption[]'. Type 'CommandSubcommandOption' is not assignable to type 'APIApplicationCommandSubcommandOption'. Types of property 'options' are incompatible. Type 'unknown[] | undefined' is not assignable to type '{ foo: string; }[] | undefined'. Type 'unknown[]' is not assignable to type '{ foo: string; }[]'. Type 'unknown' is not assignable to type '{ foo: string; }'.

typescript-bot avatar Apr 15 '22 17:04 typescript-bot

:wave: Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.


Comment by @RyanCavanaugh

:x: Failed: -

  • Type 'CommandSubcommandGroupOption' is not assignable to type 'APIApplicationCommandSubcommandGroupOption'. Types of property 'options' are incompatible. Type 'CommandSubcommandOption[] | undefined' is not assignable to type 'APIApplicationCommandSubcommandOption[] | undefined'. Type 'CommandSubcommandOption[]' is not assignable to type 'APIApplicationCommandSubcommandOption[]'. Type 'CommandSubcommandOption' is not assignable to type 'APIApplicationCommandSubcommandOption'. Types of property 'options' are incompatible. Type 'unknown[] | undefined' is not assignable to type '{ foo: string; }[] | undefined'. Type 'unknown[]' is not assignable to type '{ foo: string; }[]'. Type 'unknown' is not assignable to type '{ foo: string; }'.
  • Type '{ options?: CommandSubcommandOption[] | undefined; }' is not assignable to type 'APIApplicationCommandSubcommandGroupOption'. Types of property 'options' are incompatible. Type 'CommandSubcommandOption[] | undefined' is not assignable to type 'APIApplicationCommandSubcommandOption[] | undefined'.
Historical Information
Version Reproduction Outputs
4.2.2, 4.3.2, 4.4.2, 4.5.2, 4.6.2

:x: Failed: -

  • Type '{ options?: CommandSubcommandOption[] | undefined; }' is not assignable to type 'APIApplicationCommandSubcommandGroupOption'. Types of property 'options' are incompatible. Type 'CommandSubcommandOption[] | undefined' is not assignable to type 'APIApplicationCommandSubcommandOption[] | undefined'. Type 'CommandSubcommandOption[]' is not assignable to type 'APIApplicationCommandSubcommandOption[]'. Type 'CommandSubcommandOption' is not assignable to type 'APIApplicationCommandSubcommandOption'. Types of property 'options' are incompatible. Type 'unknown[] | undefined' is not assignable to type '{ foo: string; }[] | undefined'. Type 'unknown[]' is not assignable to type '{ foo: string; }[]'. Type 'unknown' is not assignable to type '{ foo: string; }'.

typescript-bot avatar Apr 15 '22 17:04 typescript-bot

@typescript-bot bisect good v3.8.3 bad v3.9.7

andrewbranch avatar Apr 15 '22 18:04 andrewbranch

The change between v3.8.3 and v3.9.7 occurred at b8baf4804370a4405f7f123db5bbb4530297982b.

typescript-bot avatar Apr 15 '22 19:04 typescript-bot