Skip to content

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

Closed
@Jym77

Description

@Jym77

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-repros

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions