Join Nostr
2025-03-17 16:59:48 UTC

Axel Rauschmayer on Nostr: #TypeScript – surprisingly tricky to fix: type Incrementor = { inc(): void, }; ...

#TypeScript – surprisingly tricky to fix:

type Incrementor = {
inc(): void,
};
function createIncrementor(start = 0): Incrementor {
return {
counter: start, // error
inc() {
this.counter++; // error
},
};
}

Best solution (AFAICT): Assign object to a variable, return that variable (then excess properties are allowed).
https://exploringjs.com/tackling-ts/ch_typing-objects.html#allowing-excess-properties-example-incrementor