Skip to content

Using private or protected make type checking nominal instead of structural #7755

Closed
@blakeembrey

Description

@blakeembrey

From typings/typings#380. It's definitely unexpected and I can't understand why, but I see in the specification it's intentional. For example:

export class Test {
  private method () {}
}

export class Test2 {
  private method () {}
}

export function compare (t: Test) {}

compare(new Test2())

Expected behavior:

No error, they are structurally the same. Private and protected methods can not be used outside of the class, so it shouldn't affect assignability.

Actual behavior:

index.ts(11,9): error TS2345: Argument of type 'Test2' is not assignable to parameter of type 'Test'.
  Types have separate declarations of a private property 'method'.

Edit: From the issue in typings, I also linked to #6496 and #6365 which appear to describe the issue but only dive into workarounds instead of validating why this is happening.

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions