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 Error in Combination with Discriminating Union #35700

Closed
fabb opened this issue Dec 16, 2019 · 3 comments
Closed

Type Error in Combination with Discriminating Union #35700

fabb opened this issue Dec 16, 2019 · 3 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@fabb
Copy link

fabb commented Dec 16, 2019

TypeScript Version: 3.7.2

Search Terms: discriminating union, types incompatible, not assignable

Expected behavior:

The code should compile.

Actual behavior:

Related Issues: None found yet.

Code

Note that this is a reduced example. My real-world use case uses the schema-dts json-ld library which provides the Vehicle/Car/etc. types.

type Vehicle =
    | ({
          '@type': 'Vehicle'
      } & {
          speed?: number
      })
    | ({
          '@type': 'Car'
      } & {
          speed?: number
      } & {
          roofLoad?: number
      })

let type = 'Car' as 'Vehicle' | 'Car'

// TypeScript error: Type '{ '@type': "Vehicle" | "Car"; }' is not assignable to type 'Vehicle'. Types of property ''@type'' are incompatible. Type '"Vehicle"' is not assignable to type '"Car"'.
const v: Vehicle = {
    '@type': type,
}

Interestingly, modifying the Vehicle type would make it compile (but I cannot do that in my real-world project because the types are provided by the schema-dts json-ld library):

type Vehicle =
    | ({
          '@type': 'Vehicle'
          speed?: number
      })
    | ({
          '@type': 'Car'
          speed?: number
          roofLoad?: number
      })

let type = 'Car' as 'Vehicle' | 'Car'

// TypeScript error: Type '{ '@type': "Vehicle" | "Car"; }' is not assignable to type 'Vehicle'. Types of property ''@type'' are incompatible. Type '"Vehicle"' is not assignable to type '"Car"'.
const v: Vehicle = {
    '@type': type,
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@fabb
Copy link
Author

fabb commented Dec 16, 2019

Same error on 3.8.0-dev.20191216

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Dec 18, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.9.0 milestone Dec 18, 2019
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label May 20, 2020
@rbuckton
Copy link
Member

I believe this has been addressed in the time between when this issue was filed and now, as I can no longer repro it as of 3.8.3.

@rbuckton
Copy link
Member

I believe this case was addressed by #36663

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

No branches or pull requests

4 participants