1- Return value type
Function add(first:number, second:number): number {return first + second} const total = add(1,2)Copy the code
The type is 2-void, and the return value is null
function say(): void {
console.log("122")
}
Copy the code
3-never: the command is never executed to the end
function error(): never {
throw new Error()
console.log("1111")
}
Copy the code
4- Deconstruct syntax to define types
function add({first, second}: {first:number, second:number}): number {
return first + second
}
add({first:1,second:2})
Copy the code
5 – base type Boolean, number, string, void, undfined, symbol, null
let count: number;
count = 100
Copy the code
6- Object type, {}, Class, function, []
const func = (str:string): number => {
return parseInt(str, 10)
}
const func:(str: string) => number = (str) => {
return parseInt(str, 10)
}
interface Person {
name: string
}
const q = "{"name": "jack"}";
const b: Person = JSON.parse(q);
Copy the code
7 – | or type
Let the temp: number | string = 124; temp = "1212"Copy the code
8 – array
const arr: (number | string)[] = [1,'2',3] const strArr: string[] = ['d','f','g'] const undefinedArr: Undefined [] = [undefined] const objectArr: {name: string, age: number}[] = [{name: 'Jack ', age: 26}]Copy the code
9- Type alias type
Type User = {name: string, age: number} const objectArr: User[] = [{name: 'jack', age: 26}]Copy the code
10 – tuples
const info: [string, string, number] = ['jack', 'ben', 100]
Copy the code
11-interface
Interface Person {readonly tag: string // Readonly indicates that this attribute is read-only and cannot be overwritten. Name: string, age? : number // ? [propName: string]: any const setName = (person: person, name): void => { person.name = name; } setName({name: "jack"}, "duke") const a = { name: "jack", say(){ return "kao " } } // [propName: string]: Any setName({name: "jack", sex: "niu"}, "duke") using variables to pass arguments is better than using literalsCopy the code
12- A class wants to apply an interface
class User implements Person {
name: "dragon",
say() {
return "gan"
}
}
Copy the code
13- Interfaces can inherit from each other
Teacher extends Person {teach(): string} If you are: Teacher interface Teacher extends Person {teach(): string}Copy the code
14- Defines the function interface type
interface SayHi {
(word: string): string
}
const say: SayHi = (word: string) => {
return word
}
Copy the code
15- Class definition and inherited super usage
Class Person {name: 'jack', getName() { return this.name } } class Teacher extends Person { getTeach() { return "teach" } getName() { return super.getName() + 'duke!!! '}}Copy the code
16- Private, protected, public Access type
The default type is public, allowing private types to be called inside and outside the class, allowing protected types to be used inside the class, allowing class Person {private name} to be used within the class and in its derived subclasses: string, // protected name: string, public sayHi() { this.name; console.log("name~~~") } } class Teacher extends Person { public sayBye() { this.name } } const person = new Person(); person.name = "ben"; console.log(person.name) person.sayHi()Copy the code
17- Constructor instantiation
Class Person {public name: string, constructor(name) {public name: string, constructor(name); String){this.name = name}} class Person {constructor(public name) : string){ } } const person = new Person('jack')Copy the code
18-. If the parent class has a constructor (and a value) and the subclass wants to declare a constructor, the subclass must manually call the parent class’s constructor
- Super refers to the parent class
- Super () refers to calling the constructor of the parent class
class Person {
constructor(public name: string){ }
}
class Teacher extends Person {
constructor(public age: number) {
super('jack')
}
}
const teacher = new Teacher(1000);
Copy the code
19-. If the parent class has no constructor constructor (no value), the subclass must manually call the super() null constructor
class Person { }
class Teacher extends Person {
constructor(public age: number) {
super()
}
}
const teacher = new Teacher(1000);
Copy the code
20- Static properties (getter gets, setter assigns)
- Private means private property
- An _ attribute preceded by an _ also indicates a private attribute
class Person {
constructor(private _name: string){ }
get name() {
return this._name + " ben"
}
set name(name: string) {
const realName = name.split(" ")[0]
this._name = realName
}
}
const person = new Person("duke")
person.set = 'duke ben'
Copy the code
21- Singleton mode (that is, only one instance can ever be generated)
static
Simply mount the method directly on the class, rather than on the instance of the class
class Demo { private static instance: Demo; Private constructor(public name: string) {} // Static getInstance() {if(! this.instance){ this.instance = new Demo("jack"); } return this.instance; }} const demo1 = demo.getInstance (); const demo2 = Demo.getInstance();Copy the code
22- readonly Read data but cannot be changed
class Person { public readonly name: string constructor(name: String){this.name = name}} const person = new person (" Jack ") person.name = "dragon" // Error because readOnly is setCopy the code
23- Abstract classes (pull out common things where you can set abstract methods, or actual methods and properties)
- If an abstract class has an abstract method, the extends extends extends extends extends otherwise causes a red error
abstract class Geom {
width: number;
getType() {
return "Geom"
}
abstract getArea(): number
}
class Circle extends Geom {
getArea() {
return 100
}
}
Copy the code
conclusion
React QQ group:
788023830
—-React/Redux - Old underground hero
Front-end COMMUNICATION QQ group:
249620372
—-FRONT - END - JS FRONT END
(Our mission is, for overtime, for baldness… , look up to the big guy), I hope friends to study together