Skip to content

T[K] should be never when T is never #23005

Description

TypeScript Version: 2.9.0-dev.20180329

Search Terms:
indexed access type, index type never

Code

type Example<T extends Record<'a', string>> = T['a']

type Result1 = Example<{ a: "x" } | { a: "y" }> // type of Result1 is "x" | "y"
type Result2 = Example<{ a: "x" }> // type of Result2 is  "x"
type Result3 = Example<never> // type of Result3 is any 

Expected behavior:
Type of Result3 to be never

Actual behavior:
Type of Result3 is any

Playground Link:
Link

Workaround
Now that #22042 is resolved this can be worked around with

type Workaround<T extends Record<'a', string>> = T[(keyof T) & 'a']
type Result4 = Workaround<never> // type of Result4 is never in typescript@next

Related Issues:
Similar to #22042. That was about type of T[K] when K was never while this is about when T is never.

Activity

  1. agentcooper commented on Mar 30, 2018

    @agentcooper
    type Example<T> = T extends Record<'a', string> ? T['a'] : never;

    maybe?

  2. kpdonn commented on Mar 31, 2018

    @kpdonn
    ContributorAuthor

    That actually only works because of how conditional types work with type parameters. It distributes the condition over any unions, and never is treated as an empty union. So it actually isn't that never is failing the condition there, it's that the condition is never even tested. It would actually pass the condition and end up as any if the condition is tested.

    You can see that's what is happening more clearly with something like

    type Example2<T> = T extends Record<'a', string> ? true : false;
    type Result2 = Example2<never> // never instead of either true or false

    And if you get rid of the distribution behavior by wrapping the two sides in [] you can see you still get any:

    type Example3<T extends Record<'a', string>> = [T] extends [Record<'a', string>] ? T["a"] : never;
    type Result3 = Example3<never> // any
  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions