转换 React 为TypeScript

JavaScript

import React from 'react';
import PropTypes from 'prop-types'; class Input extends React.Component { constructor(props) {
super(props);
this.state = {
value: '',
};
this.handleChange= this.handleChange.bind(this);
} handleChange(event) {
this.setState({ value: event.currentTarget.value });
} render() {
return (
<div className="input__container">
<span>{this.props.label}</span>
<input value={this.state.value} onChange={this.handleChange} />
</div>
);
}
} Input.propTypes = {
label: PropTypes.string.isRequired,
}; export default Input;

TypeScript

import * as React from 'react';

export interface IProps {
label: string;
} export interface IState {
value: string;
} class Input extends React.Component<IProps, IState> { constructor(props: IProps) {
super(props);
this.state = {
value: '',
};
this.handleChange = this.handleChange.bind(this);
} private handleChange(event: React.FormEvent<HTMLInputElement>): void {
this.setState({ value: event.currentTarget.value });
} public render(): JSX.Element {
return (
<div className="input__container">
<span>{this.props.label}</span>
<input value={this.state.value} onChange={this.handleChange} />
</div>
);
}
} export default Input;

立即捕获错误 & JavaScript可维护性。

  1. Imports

https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-default-import-interop-with-systemjs

  1. IProps & IState 接口

https://www.typescriptlang.org/docs/handbook/interfaces.html

  1. private & public

  2. code completion

这不是免费的 - 代码更冗长和更长,加上实际进行转换的成本。

如果你有一个大的现有项目,那么你和你的团队必须做出一个非常琐碎的决定,看看它是否值得转换。

https://devsandbox.io/articles/converting-react-to-typescript/


xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


转换 React 为TypeScript的更多相关文章

  1. 从零配置webpack(react+less+typescript+mobx)

    本文目标 从零搭建出一套支持react+less+typescript+mobx的webpack配置 最简化webpack配置 首页要初始化yarn和安装webpack的依赖 yarn init -y ...

  2. React与Typescript整合

    0. Typescript Typescript对于前端来说可以说是越来越重要了,前端的很多项目都用Typescript进行了重构.这主要得益于Typescript有比较好的类型支持,在编码的过程中可 ...

  3. React Hooks Typescript 开发的一款 H5 移动端 组件库

    CP design 使用 React hooks Typescript 开发的一个 H5 移动端 组件库 English | 简体中文 badge button icon CP Design Mobi ...

  4. 从零构建一个react+webpack+typescript的应用

    今天要完成在windows下从零开始构建一个react应用的任务 首先,新建一个文件夹,然后在该文件夹下使用命令npm init 初始化一个node项目. 然后安装所需依赖, npm i react ...

  5. Jest+Enzyme React js/typescript测试环境配置案例

    本文案例github:https://github.com/axel10/react-jest-typescript-demo 配置jest的react测试环境时我们可以参考官方的配置教程: http ...

  6. 超简单的react和typescript和引入scss项目搭建流程

    1.首先我们先创建一个react项目,react官网也有react项目搭建的命令 npx create-react-app my-app cd my-app 2.安装我们项目需要的样式依赖,这个项目我 ...

  7. Electron结合React和TypeScript进行开发

    目录 结合React+TypeScript进行Electron开发 1. electron基本简介 为什么选择electron? 2. 快速上手 2.1 安装React(template为ts) 2. ...

  8. [React Native] Up & Running with React Native & TypeScript

    Create a new application with Typescript: react-native init RNTypeScript --template typescript Then: ...

  9. 反压缩 js ,我的万花筒写轮眼开了,CV 能力大幅提升

    前言 因为比较菜,所以经常需要读一些别人的代码学习学习. 有源码的代码当然好,但是很多网站不开源.这些网站的 js 又都是打包压缩过的,学习起来很难受. 所以我做了一个小工具,通过修改抽象语法树,来处 ...

随机推荐

  1. 现有以下关系型数据库中的表(见表4-20表4-21和表4-22),要求将具转换为适合Hbase存储的表并插入数据。

    ① createTable(String tableName, String[] fields) 创建表,参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组.要 求当H ...

  2. 基于final shell的linux命令

    final shell操作教程: 1.查看API实时日志:cd ../../data/log/api tail -100f anyAPI-server.log2.关闭日志:control+c3.恢复实 ...

  3. https://channels.readthedocs.io/en/latest/tutorial/part_2.htmlhttps://channels.readthedocs.io/en/latest/tutorial/part_2.html

    https://channels.readthedocs.io/en/latest/tutorial/part_2.html

  4. git database 数据库 平面文件 Git 同其他系统的重要区别 Git 只关心文件数据的整体是否发生变化,而大多数其他系统则只关心文件内容的具体差异 Git 的设计哲学

    小结: 1.如果要浏览项目的历史更新摘要,Git 不用跑到外面的服务器上去取数据回来 2.注意 git clone  应指定版本,它复制的这个版本的全部历史信息: 各个分支  git init 数据库 ...

  5. pycharm设置头文件模板(for mac)

    我们要达到的效果是每次新建一个.py文件都会有头文件,关于头文件的作用之前已做解释

  6. 洛谷P2573

    Description \(n\) 个点,有各自的高度. \(m\) 条道路,有各自的长度,每条可连接两个点. 规定只能从高点走向低点,可以回到原来的某个位置走不同的道路. 求在行走道路尽量短的情况下 ...

  7. HTML5.1 新增的14项特性学习

    1.防止网络钓鱼攻击 使用target=_'blank'时, 新打开的标签可以更改window.opener.location到一些钓鱼网站,它会在开放页面上代表你执行一些Javascript代码.为 ...

  8. python中字符串的翻转(方法总结)

    Python翻转字符串(reverse string), 一共包含5种方法, 其中第一种最简单, 即步长为-1, 输出字符串; 方法如下 5种方法的比较: 1. 简单的步长为-1, 即字符串的翻转(常 ...

  9. centos设系统置语言为中文

    [root@host /]# vim /etc/sysconfig/i18n #i18n 是 internationalization 的缩写形式,意即在 i 和 n 之间有 18 个字母,本意是指软 ...

  10. java中的四种内部类使用(1)

    内部类 (一) 概述 把类定义在另一个类的内部,该类就被称为内部类. 举例:把类Inner定义在类Outer中,类Inner就被称为内部类. class Outer { class Inner { } ...