prettier: TypeScript: typing constructor breaks

Sometimes it is necessary to type the “constructor” prop of the instance of a class.

Prettier 1.11.1 Playground link

Input:

class A {
    'constructor': typeof A
    static Foo() {
        return new A()
    }
}

const a = new A()
a.constructor.Foo()

Output:

class A {
  constructor: typeof A;
  static Foo() {
    return new A();
  }
}

const a = new A();
a.constructor.Foo();

Expected behavior: Quotes around constructor should not be removed, as the current behavior leads to constructor implementation missing and removing the type declaration leads to Foo does not exist on type Function in a.constructor.Foo()

See typescript playground for errors caused

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

It seems in typescript constructor is a keyword, so it makes sense why the quotes are currently needed. It also makes sense that this could be a bug in typescript.

That said, the spec for typescript is typescript, and we are converting valid typescript to invalid typescript.

If we follow the principle of least astonishment, then prettier shouldn’t break this.

But also, in the giant typescript code bases I work with, I have never seen a real need for the syntax presented by OP (not to say it isnt needed sometimes, but its probably a low percentage of code) and thus the work around might be acceptable.

In essence, I have helped not at all 😉

But we need a test for this to make sure it won’t break again. Reopening.