TTS
TTS

Reputation: 108

Typescript declaration for exported class instance

I'm currently creating my first declaration file for a foreign library(react-native-background-timer). The library though has an default export I'm not sure how to declare in the index.d.ts file.

This is the default export of the library:

export default new BackgroundTimer();

I'm aware how to declare exported classes, but what's the best way to declare an exported class instance?

My first approach was:

declare class BackgroundTimer {
    [...]
}

declare const _BackgroundTimer: BackgroundTimer;
export default _BackgroundTimer;

Upvotes: 2

Views: 875

Answers (1)

artem
artem

Reputation: 51569

Yes this is the way to declare default export that exports an instance, it's documented on the breaking changes page for TypeScript 2.6. Previously it was possible to do it in the declaration file in one line exactly as in the code

export default new BackgroundTimer();

but having arbitrary expressions in export was disallowed.

Upvotes: 2

Related Questions