microsoft / microsoft/TypeScript

Allow use of `infer` in the type parameter of a type declaration

Open
#52,791 9 comments 23 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the motivating examples and the proposed infer syntax in the issue, then review how TypeScript currently handles type parameter constraints and conditional types. Done means type declarations can infer a type parameter directly from its constraint without repeating a conditional type, with the stated readability and validation behavior preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.