1. Strong and weak types
From a type safety perspective, there are strong and weak types.
Strongly typed arguments and passed parameters must be the same type must be the same weak type arguments and passed parameters do not have to be the same syntax does not error can be a problem during operation.
Strong typing has stronger syntactic specifications and weak typing languages allow any data to be implicitly typed where strong typing only allows no data to be implicitly typed
Javascript is a weakly typed language. Typescript is a strongly typed language and does not have implicit conversions
Static versus dynamic typing
Static type: A variable is defined as the declared type when it is declared, and the dynamic type is not changed later: It doesn’t have a type when you declare it, it only has a type when you insert data later in the process. Javascript doesn’t have a type when you define it, you assign data, you declare data, what type is a variable
There are no type restrictions, no type system, no types defined in javascript. It is flexible in small applications, but prone to problems in complex applications.
Advantages of strongly typed languages: 1. Errors can be exposed during coding; 2. Code is more obsessive and accurate; 3. Refactoring code to make it more robust 4. Reduce unnecessary type judgments
3, the Flow
The javascript type checker works by marking up some type annotations (data types) in your code and then finding type errors:Type annotation: Only data of this type can be received. This is visible in the test environment, but not at compile time. You don’t have to use flow for every data type
Flow NPM install yarn. After the installation, there are files in the bin directoryTo add a colon to a parameter name and a parameter type, you need to add an @flow flag at the top of the file.Syntax check errors can be closed.
The disadvantage is that it causes js syntax errors. Manually remove the type annotations, because the annotations just tell us during coding, You can use the official module yarn add flow-remove-type–dev To install the module. Yarn flow-remove-types is used. unmount
In the above process, the code display type error, only in the console can output, this is very inconvenient, the official provided a plug-in, vscode search. Flow Language Support can be seen directly in the code once it is installed. Type annotations can mark variable types and function return valuesAll raw data types supported by flow
Array takes an argument to specify the type in the array Flow function type restrictionMaybe: possible, mix: can accept any type of value. Any type: any valueAny is weak and mix is strong. Mix is used more in practice, and any is used to be compatible with older code.
Other types: