【问题标题】:What's the relationship of @material-ui/core and @types/material-ui?@material-ui/core 和 @types/material-ui 是什么关系?
【发布时间】:2019-02-11 03:02:40
【问题描述】:

这里我看到了一个使用 material-ui 的示例项目。

我看到我在 package.json 文件中有以下包

{
  ...
  "dependencies": {
    "@material-ui/core": "^1.4.1",
    ...
  },
  "devDependencies": {
    "@types/material-ui": "^0.20.6",
    ...
  },
  ...
}

npm install之后,我在我的项目文件夹中看到了以下目录

node_modules/@types/material-ui/
node_modules/@material-ui/core/

这是我的问题:

  1. 我想了解为什么material-ui使用@作为其包名的一部分,这种包名是不是真的意味着它是一个TS包?即,它本身带有类型信息。

  2. 如果以上是真的,那/@types/material-ui/是不是完全不需要了?

【问题讨论】:

    标签: reactjs typescript material-ui npm-install


    【解决方案1】:

    包名中的@ 并不意味着它是一个TypeScript包;普通的 JavaScript 包也可以有 @ 前缀。

    @ 前缀表示这是一个scoped package;即@material-ui/@types/ 下有一系列包。

    在这种特定情况下,@material-ui/core 包含自己的类型定义(*.d.ts 文件),因此不需要@types/material-ui,也不会实际使用。一般来说,纯 JavaScript 包需要 @types/ 包,这些包本身不包含任何类型定义。

    【讨论】:

    • 根据material-ui.com/guides/typescript,看起来我们不需要@types/material-ui,我错过了什么吗?
    • @chen 你不需要它......但我不得不将 tsconfig es5 提升到 es6
    • 他说我们不需要像npm i -D @types/material-ui那样安装,因为material-ui已经在其包中定义了类型。
    【解决方案2】:

    @types 模块被 TypeScript 用来为你提供丰富的方法和成员类型定义。如果您使用的是 TypeScript,请保留它们,否则没有必要。

    在此处阅读更多信息:https://basarat.gitbooks.io/typescript/docs/types/@types.html

    【讨论】:

      【解决方案3】:

      @ 开头的包是“限定”它们的约定。太棒了:https://docs.npmjs.com/misc/scope

      @types 范围是由庞大的DefinitelyTyped 项目提供的 TypeScript 类型定义

      【讨论】:

        猜你喜欢
        • 2020-05-21
        • 2020-04-07
        • 2021-10-19
        • 2020-07-23
        • 2021-10-24
        • 2021-08-17
        • 2020-05-04
        • 2020-04-25
        • 2016-02-13
        相关资源
        最近更新 更多