Skip to content

Protected field with same name in two classes messing up type constraint when narrowing array. #49517

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
Jym77 opened this issue Jun 13, 2022 · 7 comments · Fixed by #50328
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros

Comments

@Jym77
Copy link

Jym77 commented Jun 13, 2022

Bug Report

(I'm not even sure how to properly name that issue 🙃 )

🔎 Search Terms

typescript inheritance protected intersection

🕗 Version & Regression Information

Seen it recently in 4.6.2. Update to 4.7.3 without changing the behaviour (and 4.8.0 nightly in the playground). I don't know if it was here before.

  • I was unable to test this on prior versions because this triggered with unrelated change elsewhere in my code

⏯ Playground Link

Playground link with relevant code

💻 Code

class Foo {
  protected foo = 0;
}

class Bar {
  protected foo = 0;
}

type Nothing<V extends Foo> = void;

type Broken<V extends Array<Foo | Bar>> = {
  readonly [P in keyof V]: V[P] extends Foo ? Nothing<V[P]> : never;
};

🙁 Actual behavior

(error located at the V[P] in Nothing<V[P]>)

 Type 'Foo & V[P]' does not satisfy the constraint 'Foo'.
  Property 'foo' is missing in type 'Foo & V[P]' but required in type 'Foo'.

Changing foo from protected to public in either Foo or Bar makes the error disappear.

Removing (or renaming) protected foo = 0 from either Foo or Bar makes the error disappear.

Replacing the Array<Foo | Bar> in Broken by Array<Foo> makes the error disappear.

🙂 Expected behavior

Since V[P] is already in a type guard that is the same as the constraint for Nothing, this should not generate any typing error.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 13, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.8.0 milestone Jun 13, 2022
@RyanCavanaugh
Copy link
Member

Workaround

type Broken<V extends Array<Foo | Bar>> = {
  readonly [P in keyof V]: V[P] extends (infer T extends Foo) ? Nothing<T> : never;
};

@Jym77
Copy link
Author

Jym77 commented Jun 14, 2022

Thanks. Workaround works fine in the real code too 😄

@Jym77
Copy link
Author

Jym77 commented Jun 20, 2022

It seems that the workaround breaks api-extractor 😢

@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Jul 6, 2022
@andrewbranch
Copy link
Member

@typescript-bot bisect good v4.6.4 bad v4.7.4

@typescript-bot
Copy link
Collaborator

The change between v4.6.4 and v4.7.4 occurred at 787bb9d.

@andrewbranch
Copy link
Member

Hey this is the second bisect today that blames #48837 😅

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 7, 2022

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @Jym77

❌ Failed: -

  • Type 'Foo & V[P]' does not satisfy the constraint 'Foo'. Property 'foo' is missing in type 'Foo & V[P]' but required in type 'Foo'.

Historical Information
Version Reproduction Outputs
4.7.2

❌ Failed: -

  • Type 'Foo & V[P]' does not satisfy the constraint 'Foo'. Property 'foo' is missing in type 'Foo & V[P]' but required in type 'Foo'.

4.3.2, 4.4.2, 4.5.2, 4.6.2

👍 Compiled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Projects
None yet
4 participants