简体   繁体   English

Typescript:如何解决嵌入式定义中缺少的导出?

[英]Typescript: how to fix missing export in embedded definition?

I'm using a package from npm, let's say foo . 我正在使用npm的软件包,比方说foo This package has an embedded definiton file node_modules/foo/index.ts.d . 该软件包具有嵌入式定义文件node_modules/foo/index.ts.d However, there is an export missing from this file. 但是,此文件缺少导出。 Therefore, typescript won't let me compile, giving me the error: Module '.../foo' has no exported member 'Bar'. 因此,打字稿不会让我编译,而是出现以下错误: Module '.../foo' has no exported member 'Bar'.

How to I augment, replace or fix the definiton for the foo package? 如何增加,替换或修复foo软件包的定义?

You can use module augmentation to add the missing export. 您可以使用模块扩充来添加缺少的导出。 Simply place the following above your import statement: 只需将以下内容放在您的import语句上方:

declare module 'foo' {
    export let Bar: {
       example_name: string, // etc
    };
}
import {Bar} from 'foo';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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