Vlad Goldman
Vlad Goldman

Reputation: 91

Is there a way to wrap external type definitions into module?

Let's assume that we have type provided by some file like types.d.ts

declare function test(arg: string): void;

Is there a way to import types into own type definition and wrapped it into namespace? i would like to have something like

import { test } from 'some-weird-module/types.d.ts';

declare namespace MyModule {
    export test;
}

Upvotes: 0

Views: 595

Answers (2)

Vlad Goldman
Vlad Goldman

Reputation: 91

My requirements was completely resolved by new TypeScript 3.8 version, they introduced type only imports https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#-type-only-imports-and-export

Upvotes: 1

Romain Deneau
Romain Deneau

Reputation: 3061

Types in a .d.ts file should not need to be imported => skip import { test } from 'some-weird-module/types.d.ts';.

Then, I don't thing that it's possible to wrap these types in a namespace in one line. But, for each type to export, we can try to create a type alias that can be exported:

declare namespace MyModule {
    export type T1bis = T1; // T1 came from 'some-weird-module/types.d.ts'
    export type T2bis = T2; // T2 came from 'some-weird-module/types.d.ts'
    // etc.
}

Upvotes: 0

Related Questions