Closed as not planned
Description
π Search Terms
"type narrowing for mapped type", "type narrowing for mapped argument", "type narrowing for generic mapped argument"
β 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
I came across a case where the type could obviously be narrowed down. but for some reason typescript doesn't support this. it was a frustrating experience
π Motivating Example
Here is code example
type T = 'a' | "b"
type M = {
a: 1,
b: 2
}
function f1(a: 'a') {}
function f2(a: 1) {}
function f<TT extends T>(t: TT, p: M[TT]) {
switch (t) {
case "a": {
f1(t);
f2(p); // Argument of type '1 | 2' is not assignable to parameter of type '1'. Type '2' is not assignable to type '1'.
break
}
}
}
π» Use Cases
- I want to write more clear code without workarounds
- Current approach isn't intuitive
- I'm using type casts as workaround in the meantime