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

Intersection causes incorrect type compatability #48720

Open
webstrand opened this issue Apr 15, 2022 · 5 comments
Open

Intersection causes incorrect type compatability #48720

webstrand opened this issue Apr 15, 2022 · 5 comments
Labels
Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Needs Investigation This issue needs a team member to investigate its status.
Milestone

Comments

@webstrand
Copy link
Contributor

webstrand commented Apr 15, 2022

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

@RyanCavanaugh RyanCavanaugh self-assigned this Apr 15, 2022
@RyanCavanaugh RyanCavanaugh removed their assignment Apr 15, 2022
@RyanCavanaugh
Copy link
Member

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 RyanCavanaugh added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Apr 15, 2022
@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 15, 2022

πŸ‘‹ 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

❌ 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[]; }' 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

❌ 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
Copy link
Collaborator

typescript-bot commented Apr 15, 2022

πŸ‘‹ 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

❌ 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[]; }' 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

❌ 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; }'.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Apr 15, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Apr 15, 2022
@andrewbranch
Copy link
Member

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

@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

4 participants