Skip to content

TypeScript doesn’t properly handle a constructor with a quoted name #31020

Closed
@j-f1

Description

@j-f1

TypeScript Version: 3.5.0-dev.20190418

Search Terms: typescript quoted constructor

Code

class Foo {
    constructor(name: string) {
      this.name = name
    }
}
class Bar {
    "constructor"(name: string) {
      this.name = name
    }
}

new Foo("hello")
new Bar("hello")

Expected behavior:
Compiles without error

Actual behavior:
The new Foo call compiles properly, but the new Bar call produces a type checking error:

test.ts:9:9 - error TS2554: Expected 0 arguments, but got 1.

9 new Bar("hello")
          ~~~~~~~

Note that running the code (with the type assertions stripped) results in both constructors being called, but TypeScript’s ES5 transpilation instead sets the second constructor as a method named constructor, which causes it not to be run.

Playground Link: Open playground ↗️

Related Issues: Encountered in prettier/prettier#6063.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions