microsoft / microsoft/TypeScript

Infer declarations are allowed in generic constraints, but do not infer generic constraint types

Open
#41,040 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Inference
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 4.0.2

Search Terms: generic constraint, extends infer

Code

type InferGenericConstraint<Fn> = 
    Fn extends <T extends infer Constraint>(arg: T) => any
        ? Constraint
        : never;

type GenericFunctionExample = <T extends number>(arg: T) => void;

type InferredConstraint = InferGenericConstraint<GenericFunctionExample>;

Expected behavior:

  • Preferred: InferredConstraint is number.
  • Alternative: infer declaration is disallowed within a generic constraint clause.

Actual behavior: InferredConstraint is never

Playground Link: https://tsplay.dev/gWoYlN

Related Issues: #1213

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 by reproducing the provided code in the linked TypeScript Playground using the reported 4.0.2 behavior and compare it with the Nightly version. The issue is done when the inferred constraint is number, or when infer declarations in generic constraint clauses are rejected consistently; no repository file or test is named.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.