microsoft / microsoft/TypeScript

A trivial generic interface extending a generic type handles assignability differently than the base type

Open
#41,370 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 4.0.5, 4.1.0-beta, 4.1.0-dev.20201102

Search Terms: generic interface assignability

Code

type Assert<T, U extends T> = U;

type A = { value: 5, a: true };
type B = { value: 5, b: true };

interface Box<T> {
    value: T;
}

type _Rebox<T extends Box<any>> = Box<T["value"]>;

namespace Test0 {
    type Rebox<T extends Box<any>> = _Rebox<T>;

    export type Assertions = [
        Assert<Box<5>, Rebox<A>>, // Correctly passes
        Assert<Rebox<A>, Box<5>>, // Correctly passes

        Assert<Box<5>, Rebox<B>>, // Correctly passes
        Assert<Rebox<B>, Box<5>>, // Correctly passes

        Assert<Rebox<A>, Rebox<B>>, // Correctly passes
    ];
}

namespace Test1 {
    interface Rebox<T extends Box<any>> extends _Rebox<T> {};

    export type Assertions = [
        Assert<Box<5>, Rebox<A>>, // Correctly passes
        Assert<Rebox<A>, Box<5>>, // Correctly passes

        Assert<Box<5>, Rebox<B>>, // Correctly passes
        Assert<Rebox<B>, Box<5>>, // Correctly passes

        Assert<Rebox<A>, Rebox<B>>, // Incorrectly fails
    ];
}

Expected behavior: Test0.Assertions and Test1.Assertions should both resolve with no errors.

Actual behavior: When Rebox is an interface (in Test1), Rebox<B> is not assignable to Rebox<A>.

Playground Link

Related Issues: N/A

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 TypeScript reproduction in the issue and compare the assignability results for the type alias and interface forms in Test0 and Test1. Trace the compiler's generic-interface and assignability behavior, then add a regression test showing that Rebox is assignable to Rebox and that both assertion sets resolve without errors.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.