static - How do I point a TypeScript property to a class object in a definition file? -
for example, have class foo
static method bar
. then, have class baz
static property of qux
want point class object foo
, so:
// foo.d.ts declare class foo { static bar(name: string): void; } declare class baz { static qux = foo; }
in implementation, want use so:
// bar.ts /// <reference path="foo.d.ts" /> baz.qux.bar('hello');
see, want qux
point class object can gain access static methods without instantiating instance of it. doing this, however, gives me error saying "initializers not allowed in ambient contexts" because i'm in definition file.
is there syntax doing in typescript definition file? wasn't able find in specification (pdf).
because want qux
foo
, not instance of foo
, need use:
declare class foo { static bar(name: string): void; } declare class baz { static qux: typeof foo; } baz.qux.bar('hello');
explanation:
static qux: foo;
qux
new foo()
, static methods not accessible.
as opposed to:
static qux: typeof foo;
qux
foo
itself, not new instance - static methods accessible - not instance methods.
Comments
Post a Comment