react-native JS 迁移TS迁移步骤

2022-05-19  本文已影响0人  莲花怪兽
前置条件:npm install -g typescript和npm install -g tslint
步骤一:

分别运行下面几个命令:

yarn add --dev typescript yarn add --dev react-native-typescript-transformer yarn tsc --init --pretty --jsx react-native touch rn-cli.config.js yarn add --dev @types/react @types/react-native
步骤二:
打开tsconfig.json
{ ... // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ ... }
步骤三:
打开rn-cli.config.js,如果没有就新建一个,添加以下代码

module.exports = { getTransformModulePath() { return require.resolve("react-native-typescript-transformer"); }, getSourceExts() { return ["ts", "tsx"]; } };
步骤四:

运行以下命令:

yarn add --dev @types/jest @types/react-test-renderer
步骤五:(第三方包的使用,例如redux)

运行以下命令:

npm install -S redux react-redux @types/react-redux或

yarn add -S redux react-redux @types/react-redux
步骤七:(创建tslint配置文件)
通过tslint --init

创建tslint.json文件,之后自己配置规则
步骤八:
修改tsconfig.json文件

添加"include": ["src/**/*"], // 只检查src
步骤九:
.ts,.tsx文件引用.js文件/模块

需要在创建xx.d.ts声明文件

例如:export declare const TextColor:any;

不止是在 TypeScript 中导入未声明 JavaScript,导入.png、.json等文件时也同样需要去编写声明文件。

例如:declare module "*.json" 

declare module "*.png"
image.png

创建xx.d.ts声明文件说明可参考以下文章:https://www.jianshu.com/p/22051d562eaf

http://daief.coding.me/2018-09-04/declaration-files-of-typescript.html

上一篇 下一篇

猜你喜欢

热点阅读