Skip to content

Assignability check fails on compatible types. #44089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
h-joo opened this issue May 14, 2021 · 4 comments
Closed

Assignability check fails on compatible types. #44089

h-joo opened this issue May 14, 2021 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@h-joo
Copy link
Contributor

h-joo commented May 14, 2021

Bug Report

Assignability check for types which has same property with different types, yet where the properties are still assignable (e.g. assinging null to string[]|null) stopped to work.

I wasn't certain if this was an intended behavior or a regression.

🔎 Search Terms

  • assignability
  • generic
  • interfaces
  • extends

🕗 Version & Regression Information

  • Start to appear from 4.3.1-rc. All other versions work.

⏯ Playground Link

Playground link

💻 Code

interface A<T> {
    field : T | null;
}
type assignTo = A<string[]>;
interface assignToExtended extends A<string[]> {}

function foo () : A<null> {
    return {
        field : null
    };
}

// This consistently fails on all versions. (and I think this is intended)
const a : assignTo = foo();

// This used to work, but now starts to fail from 4.3.1-rc
const b : assignToExtended = foo();

🙁 Actual behavior

A<null> is not assignable to assignToExtended

🙂 Expected behavior

A<null> be assignable to assignToExtended

@whzx5byb
Copy link

I don't understand why const a : assignTo = foo() should fail. I think it is a bug.

Consider the example:

type Nullable<T> = { field: T | null };

declare var a: Nullable<number>;
declare var b: { field: null };
declare var c: Nullable<null>;

a = b;
b = c;
a = c; 
// ^Error here.
// Why it is an error when b is assignable to a, and c is assignable to b, but c is not assignable to a???

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 14, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.3.2 milestone May 14, 2021
@RyanCavanaugh
Copy link
Member

This seems odd; I believe we should have fallen back to a structural comparison here when the type parameter check failed.

A workaround is to write:

function foo () : A<never>

which should have identical semantics but doesn't hit this bug.

@weswigham
Copy link
Member

#40358

@RyanCavanaugh
Copy link
Member

Duping to #40312. The root cause of the break now is I believe the improved type alias preservation, which "lets" us use the more efficient (but ultimately incorrect) type parameter check rather than a structural check.

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels May 14, 2021
@h-joo h-joo closed this as completed May 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants