rn-relates icon indicating copy to clipboard operation
rn-relates copied to clipboard

快速搭建 TypeScript 和 React Native 的开发环境

Open ljunb opened this issue 7 years ago • 2 comments
trafficstars

  • 1 新建一个 React Native 项目,并 cd 到根目录;
  • 2 添加必要的依赖:
yarn add @types/react @types/react-native
yarn add --dev react-native-typescript-transformer typescript 

or

npm install --save @types/react @types/react-native
npm install --save-dev react-native-typescript-transformer typescript
  • 3 新建 tsconfig.json 文件:
{
  "compilerOptions": {
    "module": "es2015",
    "target": "es2015",
    "jsx": "react-native",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true
  }
}
  • 4 新建 rn-cli.config.js 文件:
module.exports = {
  getTransformModulePath() {
    return require.resolve('react-native-typescript-transformer');
  },
  getSourceExts() {
    return ['ts', 'tsx'];
  },
};
  • 5 新建测试页面,以 tsx 为后缀命名,修改 React 的引用方式:
import * as React from 'react';
import { Component } from 'react';

ljunb avatar Jan 17 '18 09:01 ljunb

可以使用 --template react-native init MyApp --template typescript

syanbo avatar Mar 18 '19 12:03 syanbo

@syanbo 新版功能咩,好久没关注

ljunb avatar Mar 18 '19 13:03 ljunb