question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional type not narrowed inside of conditional type

See original GitHub issue

Bug 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

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

image

🙂 Expected behavior

TName can be used to index TEvents as the same narrowing occurs.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Aug 25, 2022

TEvents[TName & keyof TEvents] is a safe workaround.

It’s not 100% identical in functionality, but another way would be

type EventArgs<TEvents extends EventType, TName extends EventName<TEvents>> = TName extends keyof TEvents ? TEvents[TName] : undefined[];
0reactions
douglasg14bcommented, Aug 25, 2022

Gotcha, thanks for that! Very helpful.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found