Skip to content

Allow use of infer in the type parameter of a type declaration #52791

@dinofx

Description

@dinofx

Suggestion

For many type declarations, it shouldn't be necessary to use a conditional type which repeats a constraint already found in type parameter.

For example, instead of:

type ConstructorParameters<T extends abstract new (...args: any    ) => any> =
                           T extends abstract new (...args: infer P) => any ? P : never;

This would be better expressed as:

type ConstructorParameters<T extends abstract new (...args: infer P) => any> = P;

🔍 Search Terms

infer type parameter conditional type type declaration

✅ 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

See above

📃 Motivating Example

TypeScript's own built-in types have other examples where constraints are repeated in both a type parameter and conditional type. I've also seen cases where developers just don't want to repeat constraints, so they define:

type RecordValueType<T> =
    T extends Record<any, infer V> ? V : never;

And then incorrect usage results in a type of never, rather than an error.

💻 Use Cases

  • Improved readability
  • Less repetition when authoring type declarations
  • Less frustration when consuming lazily-declared types

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions