Damus
hodlbod profile picture
hodlbod
@hodlbod
Does anyone else get tired of how typescript makes you annotate class members multiple times? Or am I just holding it wrong? It seems like you should only need either the class member annotations, or the constructor annotations, not both.

export type MyClassOpts = {
a: string
b: string
c: string
}

class MyClass {
a: MyClassOpts['a']
b: MyClassOpts['b']
c: MyClassOpts['c']

constructor({a, b, c}: MyClassOpts) {
this.a = a
this.b = b
this.c = c
}
}
4
Chiezo · 139w
You might write like class MyClass { constructor(readonly opts: MyClassOpts) { } } and then opts will be a member
Daniele · 139w
Typescript and classes :) Just drop both both and be happy. I do it all with functions. Classes are a liability. (and so is typescript.
Joey · 133w
TypeScript has this shortcut: https://www.typescriptlang.org/docs/handbook/2/classes.html#parameter-properties