我可以在不使用WebStorm / PhpStorm中的npm或typings的情况下安装TypeScript定义

时间:2017-03-03 13:32:03

标签: reactjs typescript react-native phpstorm webstorm

我刚刚发现我可以在此设置中安装TypeScript定义:

enter image description here

这很方便,因为我不需要使用typingsnpm来安装类型。

tsconfig.json中哪些设置使WebStorm使用这些定义?在webstorm中下载的这些库实际上有用吗?

我仍在Cannot find module react

中获得.tsx

enter image description here

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "moduleResolution": "node",
        "removeComments": true,
        "allowSyntheticDefaultImports": true,
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "./src/",
        "jsx": "react",
        "experimentalDecorators": true,
        "noLib": false,
        "declaration": false,
        "emitDecoratorMetadata": true
    },
    "exclude": [
        "node_modules",
        "src"
    ]
}

的package.json:

{
  "name": "database",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "axios": "^0.15.3",
    "lodash": "^4.17.4",
    "mobx": "^3.1.0",
    "mobx-react": "^4.1.0",
    "native-base": "^2.0.8",
    "react": "15.4.2",
    "react-native": "0.41.2",
    "react-native-autocomplete-input": "^3.1.0",
    "react-native-datepicker": "^1.4.4",
    "react-native-extended-stylesheet": "^0.3.1",
    "react-native-htmlview": "^0.5.0",
    "react-native-mobx": "^0.3.1",
    "react-native-modal-dropdown": "^0.4.1",
    "react-native-orientation": "^1.17.0",
    "react-native-pathjs-charts": "0.0.26",
    "react-native-router-flux": "^3.37.0",
    "react-native-sglistview": "^0.3.5",
    "react-native-storage": "^0.1.5",
    "react-native-view-shot": "^1.7.0",
    "typescript": "^2.2.1"

  },
  "devDependencies": {
    "babel-jest": "18.0.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-react-native": "1.9.1",
    "babel-preset-react-native-stage-0": "^1.0.1",
    "jest": "18.1.0",
    "react-test-renderer": "15.4.2"
  },
  "jest": {
    "preset": "react-native"
  }
}

2 个答案:

答案 0 :(得分:4)

Javascript| Libraries中配置的库仅供PhpStorm本身用于代码完成/导航,但它们不适用于TypeScript编译器服务。 当您通过Settings | Languages & Frameworks | Javascript | Libraries下载Typescript存根时,它们会被放置在~/.PhpStorm*/config/javascript/extLibs/中。这对于PhpStorm来说非常好,但是tsc编译器确实需要将d.ts文件放在项目目录中的某个位置。 因此,要将下载的存根可用于Typescript编译器,您需要将它们复制/移动到项目目录(并且可能重命名为更易读的人名:))。然后它们将可供PhpStorm和编译器使用。 我们计划在未来版本中提供将文件直接下载到项目文件夹(而不是system/extLibs/)的选项,请按WEB-9237进行更新。相关功能请求:WEB-14413

因此,如果您需要将Typeting用于TypeScript编译器,似乎使用npmnpm install @types/)是最佳解决方案

答案 1 :(得分:1)

你应该使用npm但是如果你坚持不这样做,我建议你创建一个typings目录,其子目录由模块名称组成,其中包含定义文件

-typings
    -react
        react.d.ts

只需确保typings目录是编译路径的一部分,即excluded中的tsconfig.json

在你的项目中使用它们的原因是这些文件包含版本化的代码,就像其他的打字稿文件一样

当您更改库版本时,定义文件很可能也需要更改,以反映新功能和不推荐使用的功能(并且您不希望npm为您管理它)