Skip to content

[7.0] Can't access field if it is protected in one constituent of an intersection (type order dependent)Β #63749

Description

πŸ”Ž Search Terms

intersection type protected member order, TS2445, instanceof narrowing protected intersection, intersection not commutative

πŸ•— Version & Regression Information

  • This changed between versions 6.0 and 7.0

⏯ Playground Link

Playground Link

πŸ’» Code

declare class Dummy {
    a: number; // Remove this error in the guard goes away
}

type Public = Base | Dummy;

declare class Base {
    protected get content(): string;
    protected set content(value: string); // Remove just this setter all errors go away
}

declare class Mock {
    get content(): string;
    set content(value: string); // Remove just this setter this all errors go away
}

declare const w: Public
if (w instanceof Mock) {
    w.content; // Error in TS 7 
}
declare const w2: Mock & Public
w2.content


declare const w3: Public & Mock
w3.content  // Error in TS 7 

πŸ™ Actual behavior

The marked lines fail to type check with:

 error TS2445: Property 'content' is protected and only accessible within class 'Mock' and its subclasses

Mock has no protected member content. The type checker miss prints the type should be Base.

It is also strange that intersection order matters Mock & Public does not error while Public & Mock

πŸ™‚ Expected behavior

Ideally no error message. In 6 accessing a field in an intersection that is protected in one of the constituents of the intersection does not fail (most of the time anyway, from what i have seen sometimes the language server shows the same error too)

If not, at least the error message should contain the correct type.

Additional information about the issue

No response

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions