React Native使用TypeScript

React Native使用TypeScript

新建项目

react-native init TSReactNative

TypeScript配置

npm install -g typescript
npm install -g typings

也可以在项目下安装typescript: 根目录下运行
npm install typescript

安装types

npm install @types/react @types/react-native

增加tsconfig.json文件,可以用如下命令生成:

tsc --init --pretty --sourceMap --target es2015 --outDir ./lib --rootDir ./ --module commonjs --jsx react

使用typescript编写组件,根目录新建demo.tsx文件

import * as React from 'react';
import {View} from 'react-native';

interface props {
    title?: any;
}

export default class  Demo extends React.Component<props> {
    render() {
        return (
            <View style = {{height: 100, width: 100, backgroundColor: 'red'}}>

            </View>
        )
    }
}

编译

在项目根目录下使用tsc命令编译文件

注意:如果出现  node_modules/@types/react-native/globals.d.ts:36:15 - error TS2300: Duplicate identifier 'FormData'.  的错误
修改tsconfig.json文件,在compilerOptions增加以下代码,即可编译成功
"lib": [
      "es2015.promise",
      "es2015",
      "es2016"
    ],  

使用

在App.js里面导入

import Demo from './lib/demo';



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值