-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 narrowing on template literal types #46045
Comments
Interesting! This one caught my eye. I was just playing around with the problem trying to solve it. Seems like I found a solution by using a type guard // With template literal types
export type Action =
| {
type: `${string}_REQUEST`;
data: number;
}
| {
type: `${string}_SUCCESS`;
response: string;
};
type RemovePrefix<T> = T extends `${infer _}_${infer Suffix}` ? `${string}_${Suffix}` : T;
function isType<T extends Action, K extends T['type']>(action: Action, type: K): action is Extract<T, { type: RemovePrefix<K> }> {
return action.type === type;
}
export function reducer(action: Action) {
if (isType(action, 'Hello_SUCCESS')) {
console.log(action.response);
}
if (isType(action, 'World_REQUEST')) {
console.log(action.data);
}
} |
Hi @anuraghazra Thanks for your efforts, you found an interesting solution that works. Unfortunately, as soon as I change the string to have multiple underscores (which occurs a lot in our application), an error shows up again:
However should someone else only have the need to support one underscore, this would be fine. Also while I feel like while in this example the overhead / added complexity would be acceptable, I wouldn't be so sure if that would still be the case when I would use it in our application where the things are already a lot more complex (since this is just a very minimal example of what we're trying to do). So I still feel like if this was either easy to add to TypeScript itself so that it could be used natively, as is the case in the example without template literal types, or if this is a bug which is easy to fix it would be much better. |
That is right, not a ideal solution, just a workaround. Cross fingers 🤞, Hope to see this land in 4.6 |
Thanks @ahejlsberg for the quick implementation! |
Bug Report
(I'm not 100% sure if this would count as a "bug report" or a "feature request", so feel free to correct me)
🔎 Search Terms
narrowing, type, template literal types, template strings, template literals
🕗 Version & Regression Information
⏯ Playground Link
Workbench Repro
💻 Code
🙁 Actual behavior
In the example with template literal types in the
reducer
function, it seems like TypeScript is not able to narrow the type ofaction
down, since accessingaction.response
results in the following error:While in the example without template literal types TypeScript is able to narrow down the type of
Action
correctly to the one withtype: 'FOO_SUCCESS'
and correctly recognizes thataction.response
can be safely accessed as expected.🙂 Expected behavior
In the example with template literal types I expect TypeScript to be able to narrow the type of
action
down in the same way as it does in the example without template literal types, and not seeing any errors.The text was updated successfully, but these errors were encountered: