C# style object initialisation in TypeScript

@RoutingKey('abc')
class Foo {
    public name: string | undefined;
    public age: number | undefined;
    public gender: string | undefined;
    constructor(init?: Partial<Foo>) {
        if (init) {
            Object.assign(this, init);
        }
    }
}

const foo = new Foo({ name: 'something' });

https://stackoverflow.com/a/72028371/1515209

Post navigation

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.