小编pas*_*luk的帖子

在 nextjs 中使用自定义 TypeScript 键入 .d.ts (未找到模块:无法解析...)

我使用的 JS 库不为 TypeScript 提供 @type 定义,所以我创建了自己的.d.ts文件。我们就这样称呼它吧foo.d.ts。我的项目结构如下所示:

...
.next/
pages/
...
typings/
    foo.d.ts
...
tsconfig.json
Run Code Online (Sandbox Code Playgroud)

我的 VS Code 对该定义没有问题,我可以在我的组件中导入,例如:

import {Foo} from "foo";
Run Code Online (Sandbox Code Playgroud)

但在运行时我在浏览器控制台中收到此错误

找不到模块:无法解析“foo”

我尝试添加

“typeRoots”:[“node_modules/@types”,“打字”]

对我来说tsconfig.json但这没有帮助。我还尝试显式添加foo.d.ts到添加的包含部分next-env.d.ts

foo.d.ts看起来像这样:

declare module 'foo' {
    declare interface ValidationError {
        message?: string;
    }
    declare namespace Foo {
        class Bar {
            constructor(config: any, displayErrorsCallback: (data: ValidationError[]) => void, onSubmitTriggered: () => void);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新(添加了 tsconfig)

{
  "compilerOptions": {
    "target": "es6", …
Run Code Online (Sandbox Code Playgroud)

typescript next.js

10
推荐指数
1
解决办法
1万
查看次数

摩纳哥编辑获得 AST 访问权限

我正在使用摩纳哥编辑器来编辑打字稿。有没有办法获得当前模型的 AST?是否可以修改树以便编辑器对更改做出反应?即我想为打字稿做简单的重构工具?

monaco-editor

5
推荐指数
1
解决办法
1155
查看次数

标签 统计

monaco-editor ×1

next.js ×1

typescript ×1