Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A more beautiful TypeScript anonymous class declaration?

Tags:

typescript

Is there a more beautiful way to express an anonymous class(?) with typed members in typeScript than this?

class Foo {
    member = {
        aNumber = <number>undefined;
        aBoolean = <bool>undefined;
    }
}
like image 965
Christopher King Avatar asked Apr 23 '13 21:04

Christopher King


2 Answers

The only anonymous alternative would be:

class Foo {
    member: { aNumber?: number; aBoolean?: bool; } = {
        aNumber: undefined,
        aBoolean: undefined
    }
}

You're usually better off just writing an interface so you can name the type.

like image 96
Ryan Cavanaugh Avatar answered Sep 19 '22 17:09

Ryan Cavanaugh


Since members are undefined by default you could simply go with:

class Foo {
    member:{aNumber:number;aBoolean:bool;} = <any>{};
}
like image 21
basarat Avatar answered Sep 18 '22 17:09

basarat