Typescript installation
npm install -g typescrip
Copy the code
Parse the TS file
tsc hello.ts //(file name)
Copy the code
Basic types of
Basic types:
boolean number string void undefined symbol null
To convert JS to a static language, you can specify variable types
// Declare variable A with type number
let a: number;
a = "hello"; // This line of code will report an error
let b: string;
b = 123;
Copy the code
Ts will report an error, but it will still compile.
Let a: number = 123;
If variable declarations and assignments are type checked
Type declarations can be used for variables and return values
function sum(a:number,b:number){
return a + b;
}
sum(123.'123'); // Type error
sum(123.456.789); // There should be 2 parameters, but 3 parameters are passed
Copy the code
Applied to the return value:
function sum(a: number, b: number) :number {
return a + "b"; // The type string cannot be assigned to number
}
sum(123.1);
Copy the code
The meaning of | or, in the range of a few
let c:boolean | string;
c = 'hello';
c = true;
Copy the code
Any: indicates any type. After a variable is set to any, type detection of TS is disabled. That is, after a variable is set to any, it can be assigned to any type
If you do not specify a type, the TS parser automatically determines that the type of the variable is any (implicit any)
Unknown: Indicates the unknown type
let a :unknown;
a = 10;
a = true;
Copy the code
Unknown types should be used as unknown as possible. Variables of unknown types cannot be copied directly to other variables
Type assertion: Can be used to tell the parser the actual type of a variable
let e: unknown;
let s: string;
e = "hello";
s = <string>e; // An error is reported without type assertion
s = e as string;// This method also works
Copy the code
Void /never specifies the return value type in the function
function fn() :boolean {
return true;
}
//void is used to indicate that no value is returned
function fn() :void {
return 123; / / an error
}
// indicates that the result is never returned
function fn() :never { / / an error
}
Copy the code
object
let b: { name: string };
b = {}; / / an error
b ={name:123}; / / an error
b = {name:'123'}
// Add? This parameter is optional
let b: { name: string; age? : number };// Specify attributes only
//[propName:string]:any indicates any attribute of any type
let c: { name: string,[propName:string]:any };
Copy the code
Restrict the parameters and return values of a function
Grammar:
(Parameter: type, parameter: type…) => Return value type
let d: (a: number, b: number) = > number;
d = function (n1, n2) {
return n1 + n2;
};
d('123'.1232); / / an error
Copy the code
Array: two declaration modes
let e:string[];
e = ['1'.1] // An error is reported. Only strings can be entered
let f:Array<string>
f = ['12'.12]// An error is reported. Only strings can be entered
Copy the code
Tuple: an array of fixed length
Enum: enumeration
enum Gender {
male,
female
}
let i: { name: string; gender: Gender };
i = {
name: "Pig Eight Quit".gender: Gender.male
};
Copy the code
Type the alias
type myType = 1|2|3|4;
let m:myType;
m=3
m=6 / / an error
Copy the code