π Search Terms
"key order when matching object types" "discriminated unions key order"
π Version & Regression Information
- This changed between versions 5.1 and 5.2
β― Playground Link
https://www.typescriptlang.org/play?ts=5.2.2#code/FAFwngDgpgBAYgewQExgXhgcgIYQgGykxgB8sEAnbAOwHMiBuUSWANSnpGwCND0sAzhACW1bAGMAFsTKZx2ChQQhMTcNBgB5EJKgV+mALbD8AaxlYA7thB7VzDQGEArgJAJDB7MIoXMAhBN7B1gYABUFTnRgGFi4sgBvGLiU2OxnZGEoanEoAC4sKAUde1TUwyL8POSylLIXNw8a2tiyAAMAMwpnYRAAfQASBMQUAF825pb2gDcOKC5eKEGE9k4eQnHJ2vblXQpl7T3xphbRrcStlPTM7NyCowUBPwArZxFbXxOWmArsKpg3BRRLQvmUzsBgOIENQ3DBZmtFgVVvN1rAMHIFEoVExIdDYVwKJwChFCfN+DAklcMlkcvlCsVpAAaZq-f5teEoxbLDkLDYTUY4qEwkAwAmcABMxMiZIwFJZlQK7LmvKWQx5qPGzKpN1p9yKFBKwAFwCAA
π» Code
type Food = 'apple' | 'orange';
type Vegetable = 'spinach' | 'carrot';
type Other = 'milk' | 'water';
type Custom = 'air' | 'soil';
type Target =
| {
audience: 'earth';
meal:
| Custom
| `fruit_${Food}`
| `vegetable_${Vegetable}`
| `other_${Other}`;
}
| {
audience: 'mars' | 'jupiter';
meal: string;
}
const vegetable: Vegetable = 'carrot';
// ok
const target: Target = {
audience: 'earth',
meal: `vegetable_${vegetable}`
};
// TS Error
// Type '{ meal: string; audience: "earth"; }' is not assignable to type 'Target'.
// Types of property 'audience' are incompatible.
// Type '"earth"' is not assignable to type '"mars" | "jupiter"'
const target2: Target = {
meal: `vegetable_${vegetable}`,
audience: 'earth'
};
Output
"use strict";
const vegetable = 'carrot';
const target = { // ok
audience: 'earth',
meal: `vegetable_${vegetable}`
};
const target2 = { // error
meal: `vegetable_${vegetable}`,
audience: 'earth'
};
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}
Playground Link: Provided
π Actual behavior
In the code both target and target2 have the correct structure based on the Target type.
But the order of keys is reversed in target2 which used to work but not anymore.
π Expected behavior
Both target and target2 should be valid.
Additional information about the issue
Discriminated unions didn't rely on the key ordering AFAIK.
π Search Terms
"key order when matching object types" "discriminated unions key order"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.2.2#code/FAFwngDgpgBAYgewQExgXhgcgIYQgGykxgB8sEAnbAOwHMiBuUSWANSnpGwCND0sAzhACW1bAGMAFsTKZx2ChQQhMTcNBgB5EJKgV+mALbD8AaxlYA7thB7VzDQGEArgJAJDB7MIoXMAhBN7B1gYABUFTnRgGFi4sgBvGLiU2OxnZGEoanEoAC4sKAUde1TUwyL8POSylLIXNw8a2tiyAAMAMwpnYRAAfQASBMQUAF825pb2gDcOKC5eKEGE9k4eQnHJ2vblXQpl7T3xphbRrcStlPTM7NyCowUBPwArZxFbXxOWmArsKpg3BRRLQvmUzsBgOIENQ3DBZmtFgVVvN1rAMHIFEoVExIdDYVwKJwChFCfN+DAklcMlkcvlCsVpAAaZq-f5teEoxbLDkLDYTUY4qEwkAwAmcABMxMiZIwFJZlQK7LmvKWQx5qPGzKpN1p9yKFBKwAFwCAA
π» Code
Output
Compiler Options
{ "compilerOptions": { "strict": true, "noImplicitAny": true, "strictNullChecks": true, "strictFunctionTypes": true, "strictPropertyInitialization": true, "strictBindCallApply": true, "noImplicitThis": true, "noImplicitReturns": true, "alwaysStrict": true, "esModuleInterop": true, "declaration": true, "target": "ES2017", "jsx": "react", "module": "ESNext", "moduleResolution": "node" } }Playground Link: Provided
π Actual behavior
In the code both
targetandtarget2have the correct structure based on theTargettype.But the order of keys is reversed in
target2which used to work but not anymore.π Expected behavior
Both
targetandtarget2should be valid.Additional information about the issue
Discriminated unions didn't rely on the key ordering AFAIK.