Skip to content

Allowing infer after extends T in conditional types would simplify complicated type definitions massivelyΒ #47330

Closed
@nth-commit

Description

@nth-commit

Suggestion

πŸ” Search Terms

  • distributed conditional types
  • infer keyword

βœ… Viability Checklist

My suggestion meets these guidelines:

  • 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 feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Doing this would allow the complete removal of an entire branch, and it's much more intuitive (the hanging infer stumped me a whole lot when I was learning).

Before:

InferringType<T> = T extends infer U ? U extends SomeConcreteType ? U : never : never

After:

InferringType<T> = T extends SomeConcreteType infer U ? U : never

Alternatives

  1. Something consistent with the as keyword in template string literal types e.g.
InferringType<T> = T extends SomeConcreteType as U ? U : never
  1. No extra syntax, just smart control flow analysis on the type variable.
InferringType<T> = T extends SomeConcreteType
  ? T // T extends SomeConcreteType on this branch
  : never // T does not extend SomeConcreteType on this branch

πŸ“ƒ Motivating Example

This playground link - has some documentation that should provide some context for the following code, but this is a pretty universal pattern anyway.

Before:

type InferEventFromTypeMatch<TypeMatch extends string> = TypeMatch extends infer InferredTypeMatch
  ? InferredTypeMatch extends Event['type']
    ? Extract<Event, AbstractEvent<InferredTypeMatch, any>>
    : InferredTypeMatch extends `${infer WildcardedTypeMatch}*`
    ? Event extends infer EventCase
      ? EventCase extends { type: `${WildcardedTypeMatch}${any}` }
        ? EventCase
        : never
      : never
    : never
  : never

After:

type InferEventFromTypeMatch0<TypeMatchString extends string> =
  TypeMatchString extends Event['type'] infer ExactTypeMatchString // Can give a much more meaningful label
    ? Extract<Event, AbstractEvent<ExactTypeMatchString, any>>
    : InferredTypeMatch extends `${infer WildcardedTypeMatch}*`
      ? Event extends { type: `${WildcardedTypeMatch}${any}` } infer WildcardTypeMatch // Can give a much more meaningful label here too
        ? WildcardTypeMatch 
        : never
      : never // Only two hanging nevers, rather than four

πŸ’» Use Cases

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions