This article mainly introduces the static type checking scheme for react projects, which has certain reference value. Now I share it with you. Friends in need can refer to it
Why type checking is needed
As a weakly typed language, JS has great flexibility, but its advantages are also its disadvantages. It can easily allow us to ignore some obscure logic, syntax errors or data type errors, even at compile time. No errors will appear to be reported during runtime, but various strange and difficult-to-solve bugs may occur.
Like
function getPrice(x) { return x * 10; } getPrice('a23') // NaN
function getDefaultValue (key, emphasis) { let ret; if (key === 'name') { ret = 'GuangWong'; } else if(key=== 'gender') { ret = 'Man'; }else if(key ==='age'){ ret = 18; } else { throw new Error('Unkown key '); } if (emphasis) { ret = ret.toUpperCase(); } return ret; } getDefaultValue('name'); // GuangWong getDefaultValue('gender', true) // MAN getDefaultValue('age', true)
This is a simple function, the first parameter key is used to get a default value. The second parameter emphasis is used to emphasize capitalization in certain scenarios. You only need to pass in true to automatically convert the result to uppercase.
But if I accidentally write the value of age as a numeric literal, if I call getDefaultValue('age', true), an error will be reported at runtime. This may occur after the business goes online, directly causing the business to become unavailable
In addition, at work, we often encounter an attribute on an object that changes after being passed between n modules. Became undefined. The above is the issue of code robustness. Another headache at work is the issue of collaboration: How to make a method provided by others produce a document that is clear at a glance? Because a project always involves the collaboration of multiple people: Classmate A wrote function a(), and classmate B had to read the API document when calling function a() to know what parameters a() requires and what parameters it will return. .
Classmate A subsequently changed function a(), but forgot to update the document. At this time, classmate C, who had just taken over the project, looked at the API document and function a() in confusion, and the problem emerged. Surface: In team collaboration, how do the provided interfaces describe themselves?
The issues involved are:
1. How does the interface describe its parameters and return values?
2. The interface parameters and return values have changed many times in countless requirement iterations, and how should the documentation corresponding to this API be updated?
4. How to describe the data format?
In order to solve many of the above pain points, we need to introduce a type checking mechanism. The so-called type checking means to detect bugs (caused by type errors) as early as possible during compilation without affecting the code running (no runtime dynamic checking is required) Type), so that writing js has an experience similar to writing strongly typed languages such as Java. It can:
make large projects maintainable
Improve efficiency. Errors are reported when writing the code instead of the compilation stage
Enhance the readability of the code and make the code the document
Enhanced Design
Using Flow
JavaScript static type checking tool produced by Facebook, it can be partially introduced without completely reconstructing the entire project, so for an existing For projects of a certain scale, migration costs are smaller and more feasible
The learning cost of using flow is also relatively low
-
Globally install the flow command line tool
npm install -g flow-bin
In the project root directory, create a .flowconfig file
Install the babel plug-in
npm install --save-dev babel-plugin-transform-flow-strip-types
Add plugin in .babelrc file
{ "presets": [ "es2015", "react", "stage-1" ], "plugins": [ "react-flow-props-to-prop-types" ] }
Install extension (⇧⌘X): Flow Language Support
Modify VS Code's default configuration for JavaScript
Code -> Preferences-> User Settings (⌘,)
Search for: javascript. validate.enable
modified to: "javascript.validate.enable": false
Use
in the project when static checking is required The file header introduces flow, such as:
/* @flow */ function getPrice(x: number) { return x * 10; } getPrice('a23') // vscode 工具提示错误
Using typescript
TypeScript is called a superset of JavaScript and is a static code launched by Microsoft. The inspection plan is encapsulated in JavaScript to encapsulate the characteristics of TypeScript. Of course, the final code can be compiled into JavaScript
1. Static type
let num: number; num = 'likely'; [ts] 不能将类型“"likely"”分配给类型“number”。 let num: number
2. Function expression
js writing method
export const fetch = function (url, params, user) { // dosomething return http(options).then(data => { return data }).catch(err => { return err }) }
The above and below are a JavaScript function. Without looking at the writing method within the method, we have no idea what pitfalls this API will have.
export const fetch = function (url: string | object, params?: any, user?: User): Promise<object | Error> { // dosomething return http(options).then(data => { return data }).catch(err => { return err }) }
The above TypeScript contains a lot of information, allowing us to easily know how to call the function
url may be of string or object type
params does not need to be passed, or any type can be passed
user is required to be of User type, of course it does not need to be passed
-
Returns a Promise, the evaluation result of Promise may be object, or it may be Error
3. Component
export interface CouponProps { coupons: CouponItemModel[]; } export interface couponState { coupons: CouponItemModel[], page: number, size: number, state: number, //可用优惠券 hasMore: boolean, isLoading: boolean, loadedError: boolean, } class CouponContainer extends React.Component<couponprops> { }</couponprops>
We can clearly know the above components It is clear at a glance which attributes, which methods, which attributes a component has, which attributes are required and which ones are optional. It truly realizes that code is a document
关于typescript还有很多其他特点,如类,接口,泛型等,具体可参考官方文档
https://www.typescriptlang.org/
项目迁移typescript
1.node
(1)使用npm安装:npm install -g typescript,当前项目使用了是v2.8.3
(2)2.2 tsconfig.json
{ "compilerOptions": { "module": "commonjs", "target": "es5", "noImplicitAny": true, "sourceMap": true, "lib": ["es6", "dom"], "outDir": "dist", "baseUrl": ".", "jsx": "react", "paths": { "*": [ "node_modules/*", "src/types/*" ] } }, "include": [ "src/**/*" ] }
(3)将.js文件改为.ts
(4)结合 gulp 进行实时编译
var gulp = require('gulp'); var pump = require('pump'); var webpack = require('webpack'); var ts = require('gulp-typescript'); var livereload = require('gulp-livereload'); var tsProject = ts.createProject("tsconfig.json"); gulp.task('compile:tsc:server', function () { return gulp.src('src/server/**/*.ts') .pipe(tsProject()) .pipe(gulp.dest('dist/server')); }); //将任务同步执行 var gulpSequence = require('gulp-sequence'); gulp.task('compile', gulpSequence( 'compile:tsc:server', )) gulp.task('watch', ['compile'], function() { livereload.listen(); gulp.watch(['./src/server/**/*.ts'], ['compile:tsc:server']); })
react
可在 webpack 配置文件添加规则
{ test: /\.tsx?$/, enforce: 'pre', use: [ { loader: "ts-loader" } ] },
3.遇到的问题
遇到的问题
动态地为global添加属性
由于js灵活的风格,我们经常动态地为某一对象添加属性,但是typeScript是编译型语言,基本原则是先定义再使用,所以当我们像下面这么引用
global.testName = '哈哈';
便会出现这样的错误
类型“Global”上不存在属性“testName”
解决方法
(1)将global强制转化为any类型 (<any>global).testName = '哈哈' (2)扩展原有的对象 global.prototy.testName = '哈哈哈' (3)使用.d.ts文件</any>
declare namespace NodeJS { export interface Global { testName: string; } }
网上很多方法是直接添加一个.d.ts文件即可,但是亲测无效,需要在引用文件引入该文件,如本项目在app.ts文件中引入了
/// <reference></reference>
Flow 与 TypeScript简单对比
总结
Flow或者TypeScript都是静态类型检查的优秀解决方案,能够给有类型检查需求的一定规模的项目带来实际收益。基于现有项目的情况,迁移 TypeScript 时间成本比较大,学习曲线相对陡峭,建议现有项目采用 Flow 方案,对于一些新的项目,可以采用 TypeScript
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
react 官网动画库(react-transition-group)的新写法
The above is the detailed content of About static type checking solution for react project. For more information, please follow other related articles on the PHP Chinese website!

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor