我想将现有的React Native应用转换为typescript
。
该文档说要卸载现有的依赖项,并用以下内容替换它们:
yarn add --dev @types/jest @types/react @types/react-native @types/react-test-renderer
我是否需要使用每个依赖项来执行此操作?如果某些依赖项没有@types/
怎么办?
这是我的package.json:
{
"name": "reactnative",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "react-native start",
"test": "jest"
},
"dependencies": {
"axios": "^0.18.0",
"expo": "^28.0.0",
"faker": "^4.1.0",
"formik": "^0.11.11",
"immutability-helper": "^2.7.1",
"lodash": "^4.17.10",
"native-base": "^2.7.1",
"react": "16.3.1",
"react-native": "0.55.4",
"react-native-actionsheet": "^2.4.2",
"react-native-autogrow-textinput": "^5.1.1",
"react-native-firebase": "^4.2.0",
"react-native-keyboard-input": "^5.2.3",
"react-native-keychain": "^3.0.0-rc.3",
"react-native-material-color": "^1.0.15",
"react-native-parsed-text": "0.0.20",
"react-native-section-list-get-item-layout": "^2.2.3",
"react-native-ui-kitten": "^3.0.1",
"react-native-vector-icons": "^4.6.0",
"react-navigation": "^2.5.5",
"react-redux-firebase": "^2.1.6",
"recompose": "^0.27.1",
"redux-logger": "^3.0.6",
"redux-promise-middleware": "^5.1.1",
"redux-thunk": "^2.3.0",
"yup": "^0.25.1"
},
答案 0 :(得分:0)
文档说要卸载现有的依赖项并替换它们
实际上,您必须同时安装软件包react
和相关类型的软件包@types/react
。如果已经安装react
,则不必卸载并重新安装。
是否需要对每个依赖项执行此操作?
只要您要输入它即可。
如果某些依赖项没有@ types /怎么办?
然后,您可以自己键入它,或者将其键入为any
,这样就不再具有Typescript的类型安全性。
答案 1 :(得分:0)
否,以@types/
开头的软件包是TypeScript 定义。如果这些软件包尚未附带TS定义(许多大型/流行库都已提供),则应在实际软件包的基础上 安装它们。您的编辑器和TypeScript编译器将使用这些定义来执行类型检查。
答案 2 :(得分:0)
@types
类型的输入。有些库包含键入(.d.ts文件),不需要键入,例如axios
。
对于没有类型的库,开发人员可以提供自己的类型声明或使用无类型的导入,例如与require
。该示例是react-native-actionsheet
的{{3}}。
文档说要卸载现有的依赖项
文档中不能这样说。现有的依赖项不应该被卸载。仅应添加各自的@type
依赖项。