user1283776
user1283776

Reputation: 21764

Move out Typescript declarations to separate file?

My file that contains both declarations and other code has become too long. Here is a part of my ./src/index.ts file:

// --> I want to move this to a separate file 
export interface Client extends Options {
    transactionsCounter: number;
    requestCallbacks: any;
    socket: any;
}
// <-- I want to move this to a separate file

export class Client {
    constructor(options: Options) {
        const defaultOptions = { 
            host: 'ws://127.0.0.1',
            port:  8080,
            logger: function() {
                const prefix = "LOG:";
                console.log.call(null, prefix, ...Array.from(arguments))
            },
            maxTime: 30000, 
            startFromTransactionId: 1
        };

        Object.assign(this, { ...defaultOptions, ...options });

        this.transactionsCounter = 0;
        this.requestCallbacks = {};
        this.socket = null; 
    }
}

What is a good way to move out TypeScript declarations to a separate file?

Do I just put it in a separate file and use imports for every declaration or is there some magic that allows me to move the declarations to another file but still have them in the namespace of my code?

Upvotes: 0

Views: 392

Answers (1)

Paleo
Paleo

Reputation: 23682

Do I just put it in a separate file and use imports for every declaration

Yep.

Notice that the IDE can help you by automatically writing imports.

Upvotes: 1

Related Questions