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
- Add test for typed constructor Closes https://github.com/prettier/prettier/issues/4111 — committed to j-f1/forked-prettier by j-f1 4 years ago
- Add test for typed constructor (#7377) Closes https://github.com/prettier/prettier/issues/4111 — committed to prettier/prettier by j-f1 4 years ago
- Add a notice to the README Merge branch 'master' of github.com:prettier/prettier into next * 'master' of github.com:prettier/prettier: (43 commits) Update `postcss-less` to v2 (#6778) Show inval... — committed to medikoo/prettier-elastic by medikoo 4 years ago
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.
@lydell The issue which discusses this https://github.com/Microsoft/TypeScript/issues/3841#issuecomment-337560146 …lol.