microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.28k stars 12.52k forks source link

Different type-inference with two structurally identical types #48116

Open hreinhardt opened 2 years ago

hreinhardt commented 2 years ago

Bug Report

🔎 Search Terms

inference identical

🕗 Version & Regression Information

3.5-3.9, 4.0-4.6, 4.7.0-dev.20220304 (did not test earlier versions)

⏯ Playground Link

Playground link with relevant code

💻 Code

type Identity<A> =
      A extends object
    ? {[K in keyof A]: Identity<A[K]>}
    : A;

type HasValue1<A> = {
    value:Identity<A>;
}
type HasValue2<A> = {
    value:Identity<A>;
}

function test<B>(a:HasValue1<B>) {}

function foo1<A>(a:HasValue1<A[]>) {test(a)} // OK
function foo2<A>(a:HasValue2<A[]>) {test(a)} // type-error

🙁 Actual behavior

The body of foo2 does not type-check because the wrong type for type-variable B is inferred.

🙂 Expected behavior

The body of foo2 should type-check exactly as foo1 since HasValue1 and HasValue2 are structurally the same type.

RyanCavanaugh commented 2 years ago

@ahejlsberg I heard you like variance issues