cgatian
cgatian

Reputation: 22994

ambient module declaration with import

Im creating an ambient module to supplement missing typings. I can create a typings.d.ts file and when I import it in other locations of the code everything works great except the return type of doSomething is any. In an attempt to fix this I imported the proper type from a known library into the typings.d.ts this supplies the proper type to the ambient module.

For some reason when I do this, the import statements of the ambient module break.

typings.d.ts

import { Tree } from '@otherLib/core' /// <=== Adding this breaks the ambient declaration below

declare module '@lib/missingTypings' {
    export function doSomething(message: string): Tree
}

In the above example I'd like to provide the proper return type of Tree from another library.

Upvotes: 2

Views: 1619

Answers (2)

gaurav5430
gaurav5430

Reputation: 13892

If you add a top level import/export your declarations are no more ambient. To make sure that you still have ambient declarations, you would need to have the import inside the declare block, but typescript currently does not allow you to use relative imports inside the declare block due to confusions with whether the module should be resolved relative to the type definition file or relative to the consumer file where the type definition is being used, and so the correct way to achieve what you are trying to do is to use the import() syntax https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html#import-types

I have written more about this here: https://stackoverflow.com/a/66768386/2054671

Upvotes: 2

Ashraf Sabry
Ashraf Sabry

Reputation: 3182

In my case this weird bug affects Visual Studio Typescript language service (the editor doesn't recognize the module) while Babel compiles the code without problems.

This answer fixed the issue for me, but I'm still not convinced why the code with static import doesn't work while dynamic import syntax works.

Upvotes: 1

Related Questions