Repository navigation
Type Error in Combination with Discriminating Union #35700
Copy link
Copy link
Closed
Labels
Needs InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Milestone
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Needs InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Activity
fabb commentedon Dec 16, 2019
Same error on 3.8.0-dev.20191216
rbuckton commentedon Jul 29, 2020
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 commentedon Jul 29, 2020
I believe this case was addressed by #36663