Conditional type not narrowed inside of conditional type
See original GitHub issueBug Report
🔎 Search Terms
nested conditional type narrow
🕗 Version & Regression Information
- This is the behavior in every version I tried (4.4 up to 4.7.4)
⏯ Playground Link
💻 Code
type EventType = {
[key: string]: unknown[]
} | string;
type EventName<TEvents extends EventType> = TEvents extends string ? TEvents : keyof TEvents;
type EventArgs<TEvents extends EventType, TName extends EventName<TEvents>> = TEvents extends string ? undefined[] : TEvents[TName];
🙁 Actual behavior
🙂 Expected behavior
TName can be used to index TEvents as the same narrowing occurs.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Documentation - Conditional Types - TypeScript
Just like with narrowing with type guards can give us a more specific type, the true branch of a conditional type will further...
Read more >Typescript conditional types not able to correctly narrow type ...
I encountered a strange issue. I'm wondering if this is genuine bug or just the lacklusterness of typescript's conditional types.
Read more >Inferring types in a conditional type - Learn TypeScript
When item1 is constructed, the condition in the conditional type is true because number[] matches (infer E)[] . E is therefore inferred to...
Read more >The Power and Limitations of Conditional Types and the “infer ...
Not really useful in most cases, but types are about narrowing down ... The infer keyword can go inside of a conditional type...
Read more >Conditional types in TypeScript - Artsy Engineering
Let's add basic types to this function so we can let TypeScript worry about whether we are using it safely or not. function...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
TEvents[TName & keyof TEvents]
is a safe workaround.It’s not 100% identical in functionality, but another way would be
Gotcha, thanks for that! Very helpful.