#How to get class interface in its constructor?
8 messages · Page 1 of 1 (latest)
Preview:```ts
class Foo {
name?: string
weight?: number
constructor(data: InstanceType<typeof Foo>) {
Object.assign(this, data)
}
}
const f1: Foo = new Foo({name: "Fooo"})
console.log(f1)```
You can choose specific lines to embed by selecting them before copying the link.
I need to initialize class like this
To set all the data at once. It's needed in the API transformer layer
Sure, I don't want to declare the interface separately because it's literally a double input
I can think of:
constructor(data: unknown) {
const dataTyped = data as InstanceType<typeof Foo>;
Object.assign(this, dataTyped);
}
Preview:```ts
class Foo {
name?: string
weight?: number
constructor(data: Foo) {
Object.assign(this, data)
}
}
const f1: Foo = new Foo({name: "Fooo"})
console.log(f1)```
You can choose specific lines to embed by selecting them before copying the link.
👆🏻