node.js - TypeScript - 无法在其他类型根目录中的类型覆盖中导入自身,TS7016

标签 node.js typescript feathersjs

我正在使用 FeathersJS 和 TypeScript。 由于它仍在开发中(TS for Feathers),我发现我需要重写类型才能适应我的情况(例如,可以禁用或启用分页,类型应该处理它但目前不处理) .

在我的 tsconfig.json 中,我有以下内容:

"typeRoots": [                            /* List of folders to include type definitions from. */
      "./types",
      "node_modules/@types"
    ],

顺序并不重要,因为我也首先尝试过使用 node_modules。

我将文件夹 feathersjs__feathers 放在 ./types 中,只有 index.d.ts(删除了 package.json) 以及 DefinitelyTyped 类型的精确副本。

这一行导致了问题:import * as self from '@feathersjs/feathers';

TS7016 could not find a declaration file for module '@feathersjs/feathers'. 'C:/Users/marek/dev/system/api — kopia/node_modules/@feathersjs/feathers/lib/index.js' implicitly has an 'any' type.   Try npm install @types/feathersjs__feathers if it exists or add a new declaration (.d.ts) file containing declare module 'feathersjs__feathers';

我尝试将 package.json 添加到我的文件夹中,就像这样:

{
  "name": "@types/feathersjs__feathers",
  "version": "0.0.1",
  "types": "index.d.ts"
}

但这对解决这个问题没有帮助。 如果我安装 @types/feathersjs__feathers 所以它在 node_modules 中没有错误,但无论 typeRoots 中的顺序如何,它都从那种类型,而不是来 self 的覆盖。

有什么解决方案吗?

通常带有 ./types 的配置和带有 index.d.ts 的文件夹都很好,例如对于 feathers-mongoose 我在那里有自己的打字,它们工作正常。问题在于导入 self ...

最佳答案

typeRoots只帮助 TypeScript 编译器找到要为 types 加载的文件选项(或 /// <reference types="..."/> 指令)为了任何 declare module "..." { ... } 的利益以及它们包含的全局声明。但是 index.d.ts @feathersjs/feathers 的文件不使用 declare module "..." { ... } ;它旨在通过 TypeScript 的主要模块解析过程作为模块本身找到。该过程不遵守 typeRoots并且很难查看 node_modules/@types当它在 node_modules 中找到未类型化的模块时.要让模块分辨率看起来在一个额外的地方,你必须使用 baseUrlpaths选项:

{
    "compilerOptions": {
        // ...
        "baseUrl": ".",
        "paths": {
            "@feathersjs/feathers": ["types/feathersjs__feathers"]
        }
    }
}

这对我来说似乎是糟糕的设计,但是 the TypeScript team has already said once that the behavior is intentional ,所以我认为不值得提交错误来要求他们更改它。

(我注意到 Stack Overflow“相关”小部件找到了 an answer,它基本上回答了你的问题,尽管在我看来它不像我的回答那么清楚。)

关于node.js - TypeScript - 无法在其他类型根目录中的类型覆盖中导入自身,TS7016,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52463716/

相关文章:

javascript - NodeJS : How to handle a variable number of callbacks run in parallel and map their responses to requests?

javascript - 如何在 node.js 中发送巨大的数据池作为响应?

javascript - 无法读取未定义的属性 'setData' - jQuery UI Touch Punch

javascript - 羽毛- Mongoose : Get by custom attribute in feathers-mongoose

javascript - 不能在带有类验证器的父类的构造函数内部使用验证

node.js - Sails.js - PATH 变量 - 无法识别 sails 命令

javascript - 为什么 Redux.js "Dispatch"TypeScript 接口(interface)有模板参数?

javascript - 设置一个 typescript 库以在 Nodejs、浏览器和其他 Typescript 项目中使用

node.js - 如何从钩子(Hook)访问 Mongoose 模型

javascript - 如何用 Mongoose 在羽毛中创建单例资源?