node.js - 找不到声明文件时如何使用模块
问题描述
我有一个使用 node.js 的打字稿项目。有一个模块我想使用 npm 包 country-code-lookup。
问题是它没有支持类型声明。但是,我仍然想使用它。
有没有可能我仍然可以在没有打字的情况下使用这个包。
import * as CountryCodeLookup from "country-code-lookup";
const countryCodes = CountryCodeLookup.countries;
console.log(countryCodes);
打字稿尝试编译时出现以下错误。
TS7016:找不到模块“国家代码查找”的声明文件。'/Users/kay/portal/node_modules/country-code-lookup/index.js' 隐含了一个 'any' 类型。尝试
npm install @types/country-code-lookup
它是否存在或添加一个新的声明 (.d.ts) 文件,其中包含declare module 'country-code-lookup';
解决方案
如果您只需忽略此库的所有类型检查功能就没有问题,您有两种选择:
- 添加
// @ts-ignore
最重要的导入,如下所示:
// @ts-ignore
import * as CountryCodeLookup from "country-code-lookup";
- 创建一个带有
any
类型的声明文件,因此所有导入都被自动认为是any
类型的。
为此,请创建一个文件src/types/country-code-lookup/index.d.ts
添加以下声明:
// country-code-lookup/index.d.ts
declare module 'country-code-lookup';
在此文件中,稍后您可以添加自己的类型定义。如果他们足够好,请推动他们做DefinitiveTyped,以便所有社区都可以使用和改进它!
推荐阅读
- sql - SQL (MS SQL Server) 返回列值作为乘法的结果
- python - 使用多处理加载 Pandas 数据框
- python-3.x - 更新和解决 Google 表格中的评论
- javascript - Node.js 单线程机制
- python - 根据一些重复的索引选择 3d 张量的行。棘手的切片
- c - 运算符如何在表达式优先级方面工作?
- marklogic - MarkLogic 数据中心安装和升级
- java - java8如何转换Future
到 CompleteableFuture - python-3.x - PermissionError: [Errno 13] Permission denied based on file size
- c# - 在 .net core web api 中实现基于用户的授权过滤器