gpt4 book ai didi

typescript - 从 TypeScript 中的 node_modules 文件夹导入声明文件

转载 作者:搜寻专家 更新时间:2023-10-30 21:59:20 24 4
gpt4 key购买 nike

我从包管理器“npm”下载了包“immutable”,它的文件“package.json”有一个属性“typings”,值为 node_modules/immutable/dist/immutable.d.ts

问题是当我尝试从类中使用它时出现错误 TS2307 (TS) Cannot find module 'immutable'

import { Immutable } from "immutable"; // error here

export class RestRepository<T = IEntityBase> extends BasicRepository<T>
{
private c: Immutable.Foo;
}

我知道 tsconfig 中的属性 exclude 排除了 node_modules 文件夹中的所有文件(node_modules/@typings 除外)但是“@typings/immutable "没有 ts 文件,只有一个 readme.md

README.md

This is a stub types definition for Facebook's Immutable (https://github.com/facebook/immutable-js).

Facebook's Immutable provides its own type definitions, so you don't need @types/immutable installed!

如有任何帮助,我们将不胜感激!

-tsc 2.6

最佳答案

我在使用 Immutable 时遇到了类似的问题。我可以解决它的唯一方法是执行以下 3 个步骤:

1) 将 immutable.d.ts 文件复制到应用程序的根目录,在我的例子中,它与我的 index.ts 处于同一级别

2) 像上面那样导入不可变的命名空间(第一步防止显示错误)

3) 导入我实际想要使用的函数,即 map 等

这给我留下了以下内容:

import * as Immutable from "immutable";
import { Map } from "immutable";

只有这样我才能摆脱命名空间导入的错误(对我来说这是阻止发布构建)并导入我尝试使用的实际功能 (Map)。

关于typescript - 从 TypeScript 中的 node_modules 文件夹导入声明文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47289857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com