简体   繁体   中英

How to use local typings for plain JS files in TypeScript

I have a plain JS module (vendored) that exports a function:

In ./vendor/my-lib.js :

export function doStuff(mandatoryArgumentX, optionalArgumentY) {
}

I also have typings for it - in ./vendor/my-lib-typings.d.ts .

export function doStuff(mandatoryArgumentX: number, optionalArgumentY?: string)

Now, I have a TypeScript module that wants to use it:

import {doStuff} from '../vendor/my-lib';
doStuff(44);

How do I get my TypeScript module to use the typing when dealing with this import? Global modules (imported with import 'my-lib' ) are easy, because I can just put the same module name in typings. It's not the case here though, since the paths are really dynamic.

Is there anything I can put in any of these 3 files that will solve this puzzle (besides rewriting JS to TS)?

如果您在.d.ts文件和.js文件之间使用相同的名称,则它应该可以工作-因此,只需重命名.d.ts文件,就可以了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM