catalog
- Flow overview
- Flow VS. TypeScript
- Flow installation
-
Flow usage
- 1. Use comments at the beginning of the file
- 2. Use type annotation in code
- 3. In package.json Added in,
- 4. Initialize flow
- 5. Execute the flow command to check
- 6. Close the flow command
-
Flow compilation
-
1. The official website provides flow remove types
- NPM environment
- Yarn environment
- 2.Babel
-
-
Flow development tool plug in
- Vscode flow language support
- Other editors
- How to use flow syntax
Flow overview
W3cschool flow official reference document
Flow
yesJavaScript
The static type checker is implemented by static type inference. yes2014
Year by yearFaceBook
Launched a tool, using it can make up forJavaScript
Weak type brings some disadvantages. It can be said to beJavaScript
It provides a more perfect type system. stayReact
andVue
In all the projectsFlow
The use of.
JavaScript
It is a dynamic type checking language. The code checks whether the type is correct during execution,C#
andJava
Are static type checking, check whether the type is correct when compiling the code. useflow
You can make itJavaScript
LikeC#
andJava
The same development experience.
Mark the type of each variable or parameter by adding comments to the code, and then
Flow
According to these annotations, you can check the code exception, so as to check the type exception in the development phase.
- The colon of a parameter is called a type annotation, which is passed when it is published
Babel
The annotation is eliminated, so the production environment will not be affected.- It is not required that all variables and parameters should be annotated
any
Type.
function sum (a: number, b: number) {
return n * n
}
sum(100, 50)
Sum ('100 ', 50) // can detect exceptions
Flow VS. TypeScript
Flow
AndTypeScript
They’re all static type checkers,TypeScript
They are more powerful, but they are allJavaScript
Supersets based onJavaScript
In the end, they all need to be compiled intoJS
function. Generally, large projects need static type checking to ensure code maintainability and readability,Vue2.0
The source code introducedflow
,flow
You can make your code use dynamic type checking with minimal changes, whileVue3.0
Already used inTypeScript
Developed.
Flow installation
npm flow-bin
npm init -y
npm i flow-bin --dev
Can be innode_modules
Of.bin
See in the directoryflow
We can execute it on the command lineflow
, which is used to check the type exception in the code in the project.
Flow usage
1. Use comments at the beginning of the file
@flow
It’s a good sign, like thisflow
To check this file.
2. Use type annotation in code
PS: I see it here
VSCode
There’s going to be something in itjs
Syntax check of, need to close manuallySettings – >
javascript valida
->ClosingJavaScript
verification
2. In package.json Added in,
"scripts": {
"flow": "flow"
}
If it isyarn
Direct operationyarn flow
It can be set or notscripts
directnpx flow
function
3. Initialize flow
Run the following statement to generate another one in the same level directory.flowconfig
Configuration file for
npm run flow init
# > [email protected] flow E:\professer\TypeScript
# > flow "init"
4. Execute the flow command to check
npm run flow
# > [email protected] flow E:\professer\TypeScript
# > flow
# Launching Flow server for E:\professer\TypeScript
# Spawned flow server (pid=5816)
# Logs will go to C:\Users\AppData\Local\Temp\flow\EzCzBprofesserzBlagouzBTypeScript.log
# Monitor logs will go to C:\Users\AppData\Local\Temp\flow\EzCzBprofesserzBlagouzBTypeScript.monitor_logNo errors!
5. Close the flow command
npm run flow stop
# > [email protected] flow E:\professer\TypeScript
# > flow "stop"
# Trying to connect to server for `E:\professer\TypeScript`
# Told server for `E:\professer\TypeScript` to die. Waiting for confirmation...
# Successfully killed server for `E:\professer\TypeScript`
Flow compilation
becauseflow
The type annotation for is notjavascript
It can’t run directly because of its standard syntax. We need to translate the code into the original code that the browser can executeJavaScript
Language running. So we can remove the annotation we added after coding.
1. The official website provides flow remove types
npm flow-remove-types
NPM environment
1) Installation
npm i flow-remove-types --dev
2) package.json
Modify configuration in
#After compiling in SRC directory, transfer to dist directory
"scripts": {
"flow": "flow",
"flowRemove": "flow-remove-types src/ -d dist/"
}
3) Operation
npm run flowRemove
You can see in thedist
Directory with the compiledjs
file
Yarn environment
1) Installation
yarn add flow-remove-types
2) Operation
#The first is the specified directory, and - D is followed by the output directory
yarn flow-remove-types src -d dist/
2. Babel
1) Installation
#We can use the Babel command to compile @ Babel / cli
#@ Babel / perset flow contains the plug-in for our flow type checking
yarn add @babel/core @babel/cli @babel/perset-flow
npm
Environmental Science
npm i @babel/core @babel/cli @babel/perset-flow --dev
2) Add profile
Peer directory add.babelrc
File, add configuration
{
"presets": ["@babel/preset-flow"]
}
3) Use
yarn
Environmental Science
yarn babel src -d dist
npm
Environmental Science
staypackage.json
Add in
"scripts": {
"babel": "babel src/ -d dist/"
}
functionbabel
npm run babel
Flow development tool plug in
The exception is not displayed in the terminal, but directly displayed in the development tool.
Vscode flow language support
You can see and modify it directly in the code
Other editors
flow-editors
How to use flow syntax
Flow (2) — simple syntax usage