typescript - 根 index.d.ts 中子文件夹的类型

标签 typescript material-ui definitelytyped

我们为 material-ui@next 创建了类型,并希望将它们与我们在上一个 beta 中所做的库一起发布。

这是 index.d.ts 的链接.

但是这些类型在当前形式下是不可用的。在开发中,它们在本地使用并且运行良好,但是当使用库 TypeScript 发送文件时,它们似乎使用了不同的发现策略。

所有引用子文件夹的类型(例如 declare 'material-ui/Button/Button')都不会被发现是 TypeScript。导入组件时会出现错误:

[ts]
Could not find a declaration file for module 'material-ui/Button/Button'. '<project path>/node_modules/material-ui/Button/Button.js' implicitly has an 'any' type.
  Try `npm install @types/material-ui/Button/Button` if it exists or add a new declaration (.d.ts) file containing `declare module 'material-ui/Button/Button';`

npm_modules 文件夹中使用时,TypeScript 是否不接受声明其他导入?因为如前所述,在本地使用它们或什至将它们移动到 @types/material-ui 将使它们工作。

此外,TypeScript 似乎找到了 index.d.ts,因为从“root”导入有效(import {Button} from 'material-ui') .

最佳答案

因此,事实证明 TypeScript 处理 node_modules/@types/*node_modules/* 中的类型略有不同:

@types 中的类型称为增强模块。它们是全局的,其中的所有内容都将在您的项目代码中。这就是为什么访问子模块声明(如 material-ui/Button/Button)有效。

常规 npm 模块被视为(常规)模块。因此,如果您导入一个子文件夹,该子文件夹必须包含类型。 TypeScript 不会转到模块的根目录并检查那里的类型是否有扩充模块。

这意味着您 (a) 必须将您的类型发布到 DefinitelyTyped 或为每个子文件夹和文件创建 .d.ts 文件。

您可以在这里找到更长的解释:https://github.com/Microsoft/TypeScript/issues/17945

关于typescript - 根 index.d.ts 中子文件夹的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45804916/

相关文章:

node.js - ES6 : Module '"mongoose"' has no default export

node.js - @openapi 在 typescript 项目中无法工作/无法识别

javascript - React : TypeError: Object(. ..) 不是 Material UI 中的函数

reactjs - Typescript + React,从无状态功能组件渲染数组

angular - 在 Angular 组件中配置 Revolution Slider

typescript - NestJs JWT 身份验证返回 401

javascript - 页面刷新后,Material UI Button 失去样式

reactjs - Material UI - 排版 fontSize

typescript - 如何为 umd 库创建 TypeScript 定义文件 (d.ts)

javascript - 使用 gulp-typescript 配置类型