Global Feed Post Login
Replying to Avatar 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

}

}

Avatar
Chiezo 2y ago

You might write like

class MyClass {

constructor(readonly opts: MyClassOpts) {

}

}

and then opts will be a member

Reply to this note

Please Login to reply.

Discussion

Avatar
hodlbod 2y ago

Ah, thank you, that might be what I was after

Thread collapsed