|
|
|
|
|
by randomdata
1103 days ago
|
|
JavaScript has half decent object construction syntax that avoids the nesting hell of classes: function Foo(s) {
this.s = s
}
Foo.prototype.bar = function() {
console.log(this.s)
}
const baz = new Foo("Baz")
baz.bar()
But it seems Typescript can't handle it? |
|
(1) imperative style mutation of the prototype is runtime dependent, and typescript can't really rely on runtime behavior.
(2) Foo has two incompatible types in JS, it is both `(s) => void` and `new (s) => Foo`, typescript by default will assume the first signature (which is usually what people want).
In any case, you can specify the types (though in 99% of the cases, just use a class):