Repository navigation
Support for switch statement shorthandΒ #61038
Copy link
Copy link
Closed as not planned
Labels
SuggestionAn idea for TypeScriptAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it addsAn issue which adding support for may be too complex for the value it adds
Description
π Search Terms
"switch shorthand"
β Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
Support for type narrowing in
const result = {
1: checkThis.one
2: checkThis.two
} [checkThis.number] || checkThis.moreπ Motivating Example
Imagine you need to narrow type of object from 2 possible options and access property based on that type. Easy solution - ternary.
Now another option is added. Easy solution - extend ternary by another check.
And few more options are added. Extending ternary any more will make it hard to read. Switch statement is easier to read, but it requires to declare variable first and then assign something to it based on condition.
But shorthand switch exists, similar to ternary. And it is not working with typescript.
Playground link or as code:
type Fruits = {
plantType: "fruit";
fruitList: string[];
};
type Vegetables = {
plantType: "vegetable";
vegetableList: string[];
};
type Berries = {
plantType: "berry";
berryList: string[];
};
type Plants = Fruits | Vegetables | Berries;
const testPlant = {
plantType: "fruit",
fruitList: [],
} as unknown as Plants;
const selectedListError = {
fruit: testPlant.fruitList,
vegetable: testPlant.vegetableList,
berry: testPlant.berryList,
}[testPlant.plantType];
const selectedListWorks =
testPlant.plantType === "fruit"
? testPlant.fruitList
: testPlant.plantType === "vegetable"
? testPlant.vegetableList
: testPlant.berryList; Property 'fruitList' does not exist on type 'Plants'.
Property 'fruitList' does not exist on type 'Vegetables'.
Property 'vegetableList' does not exist on type 'Plants'.
Property 'vegetableList' does not exist on type 'Fruits'.
Property 'berryList' does not exist on type 'Plants'.
Property 'berryList' does not exist on type 'Fruits'.
π» Use Cases
Main use case: one line switch statement.
Two workarounds: normal switch or long ternary.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
SuggestionAn idea for TypeScriptAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it addsAn issue which adding support for may be too complex for the value it adds
Activity
nmain commentedon Jan 24, 2025
It's not quite like a switch though, is it? In particular, all branches are executed because the whole object literal is evaluated before an individual property is plucked. So there's no narrowing Typescript can do here.
Playground
jcalz commentedon Jan 24, 2025
Right, if you want this to actually be a
switch-substitute then you need to use something likewhich probably isn't "shorthand" anymore, and effectively reduces this feature request to #30581, the solution to which is #47109, refactor to generics like
and that's not pretty or anything, so I could imagine wanting something more convenient if this pattern were widely used, and... well, this is the first I've seen of it.
RyanCavanaugh commentedon Jan 24, 2025
This falls apart as a "switch" replacement as soon as the code gets even modestly more complex, e.g. this code always crashes:
I don't think it's worth adding a new kind of narrowing to support this
typescript-bot commentedon Jan 27, 2025
This issue has been marked as "Too Complex" and has seen no recent activity. It has been automatically closed for house-keeping purposes.