1. Javascript is weakly typed and dynamically typed because it allows implicit type conversions. Missing the reliability of the type system. So in large projects, we usually use TypeScript.
What advantages does TypeScript have over JavaScript?
By providing type constraints that make it more controllable, easier to refactor, more suitable for large projects, and easier to maintain,TypeScript dramatically improves the reliability of code. Type syntax errors are found during compilation, not at run time.
2. Strong versus Weak typing (Type safety)
- Strongly typed: The language level restricts functions to have the same type of argument as the parameter type, with stronger type constraints. Strongly typed languages do not allow arbitrary hermite-type conversions.
- Weak typing: The language level does not restrict the types of arguments. There are few constraints. Weakly typed languages allow arbitrary implicit type conversions.
Advantages of strong typing:
- ① Errors are exposed earlier and can be found at the coding stage
- ② The code is more intelligent and the coding is more accurate. Knowing the type of the variable allows the compiler to give hints.
- ③ Refactoring is more robust
- ④ Reduce unnecessary type judgment
3. Type system (static versus dynamic)
- Static typing: when a variable is declared, its type is unambiguous; after it is declared, its type cannot be changed.
- Dynamic typing: The type of a variable is known at run time and can change at any time.
4.Flow:JavaScript type checker;
Json yarn init --yes // install flow-bin yarn add flow-bin --dev // Install the flow initialization file yarn flow initCopy the code
A :Number,b: Number. Then run YARN Flow to check the type error. If any type error occurs, the terminal displays a detailed error message.
@flow function sum(a:Number,b: Number){return a + b} sum(100,100)Copy the code
5.TypeScript installation and configuration
Install command
// Initialize a package.json yarn init --yes // install typescript yarn add typescript --dev // generate an equivalent JS file and feedback an error yarn TSC file name at the terminalCopy the code
Add a type to name using TypeScript.
TypeScript configuration files
Json file yarn TSC --init // The error is displayed in Chinese format yarn TSC --locale zh-cnCopy the code
6.TypeScript common types
Void (void); void (void); void (void); void (void); void (void); void (void); It can only be undefined in strict mode.
const e:viod = null/undefined
Copy the code
② Object, its type can be an object, an array, or a function
const foo: object = function () {}//[]//{}
Copy the code
③ An array of pure numbers
Const arr1:Array<number> =[1,2,3] const arr2:number[]=[1,2,3]Copy the code
Tuple type: specify the number of elements and element type of the array.
const tuple:[number,string]=[18,'czd']
const age = tuple[0]
const name = tuple[1]
Copy the code
⑤ Enumeration type, will generate bidirectional key-value object.
enum Color {
Red,
Green,
Blue
}enum Color {
DarkRed = 3,
DarkGreen,
DarkBlue
}
Copy the code
Var Color; (function (Color) { Color[Color["Red"] = 0] = "Red"; Color[Color["Green"] = 1] = "Green"; Color[Color["Blue"] = 2] = "Blue"; })(Color || (Color = {})); (function (Color) { Color[Color["DarkRed"] = 3] = "DarkRed"; Color[Color["DarkGreen"] = 4] = "DarkGreen"; Color[Color["DarkBlue"] = 5] = "DarkBlue"; })(Color || (Color = {}));Copy the code