microsoft / microsoft/TypeScript

Module augmentation allows non-identical type parameters as long as there are defaults, but doesn't pass them

Open
#55,026 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Binder Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms
  • module augmentation
  • callback generic type
🕗 Version & Regression Information

Currently using typescript 5.1.6, but AFAIK this behavior was the same in previous versions. This also occurs on the Nightly.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics (and the rest of the FAQ)
⏯ Playground Link

Playground link with relevant code

💻 Code
interface Chainable<Subject = any> {
    within(fn: (prevSubject: Subject) => Chainable<Subject>): Chainable<Subject>;
}

interface Chainable<Subject = any, ExtraType = undefined> {
    within(fn: (prevSubject: Subject, extra: ExtraType) => Chainable<Subject>): Chainable<Subject, ExtraType>;
}

interface ChainableLike<Subject = any, ExtraType = undefined> {
    within(fn: (prevSubject: Subject) => Chainable<Subject>): Chainable<Subject>;
    within(fn: (prevSubject: Subject, extra: ExtraType) => Chainable<Subject>): Chainable<Subject, ExtraType>;
}

type ChainableWithSecondType = Chainable<string, string>;
type Wrong = ChainableWithSecondType['within'];
type Correct = ChainableLike<string, string>['within'];
🙁 Actual behavior

If you omit the default on the added generic ExtraType, you get an error: All declarations of 'Chainable' must have identical type parameters. But when there's a default, it allows the type parameters to be different, but doesn't properly infer types.
image

Typescript does not produce any errors, but when you mouseover Wrong, it shows the type of extra to be ExtraType.
image

🙂 Expected behavior

Either TS should completely ban adding an optional extra type parameter, or it should properly infer type (i.e. mousing over Wrong should produce the same as mousing over Correct)

image

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

Reproduce the behavior using the linked TypeScript Playground and compare the Wrong and Correct types in the supplied module-augmentation example. Trace how the compiler checks differing generic parameter lists when added parameters have defaults, then add coverage for the reported behavior and verify that optional extra parameters are either rejected or inferred consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.