Skip to content

Assertion signature not working on private identifier #42166

@ajafff

Description

@ajafff

TypeScript Version: 4.2.0-dev.20201231

Search Terms: private assert

Expected behavior:

Both methods work the same, no compile error.

Actual behavior:

Error on this.#assert: Assertions require every name in the call target to be declared with an explicit type annotation.(2775)

AFAICT this is caused by getTypeOfDottedName using getPropertyOfType with property name #assert, which doesn't work for private identifiers. Instead is looks for a regular property with name '#assert'. If I add '#assert': (v: any) => asserts v = (v) => {}; the error goes away.

Code

class C {
  assert: (v: any) => asserts v = (v) => {};
  #assert: (v: any) => asserts v = (v) => {};

  works(v: boolean) {
    this.assert(v);
    v;
  }

  doesntWork(v: boolean) {
    this.#assert(v);
    v;
  }
}

Playground Link: Provided

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions