|
|
|
|
|
by chuckadams
800 days ago
|
|
Typescript is best when you don't use `class` or `this` at all, but instead return object literals of static closures. In other words, more like this: function makeCounter(initial = 0) {
let count = initial;
function get() { return count }
function inc() { count += 1 }
function dec() { count -= 1 }
return {get, inc, dec}
}
// works just like a normal object
const ctr1 = makeCounter();
ctr1.inc()
// but this works too. try doing this with a class instance.
const {get, inc} = makeCounter()
inc()
This is the typical style of most VueJS code (though it would replace count with a ref and probably call the function `useCounter`). TypeScript is smart enough that it can infer interfaces and even classes from such literals should you go that way, full support for substitutability checking and all. |
|