I. Concept of type

The type function is to give a new name to a type. Basic types, union types, and tuples can be used to define a new name for a type

let nums: test = 12; console.log(nums); type obj = {name:string}; // let obja:obj = {name: '23'} console.log(obja); type add = (x:number,y:number) => number; // let add:add = (a,b) => a + b; The console. The log (add (1, 3)); type data = [number,string]; // let ar: data = [23,'ni']; console.log(ar); type numstring = test|string; // let numstr:numstring = 32; console.log(numstr);Copy the code

The difference between type and interface

1. Type, like interface, can be used to describe types between objects or functions.

name: string, age: number, say(s:string,y:string): string, }; Interface People {name: string, age:number, say(x:string,y:string):string} let testObj:User = {name: '四', age: 23, say (x, y). {return x + y}} the console log (testObj. Say, 'you', 'I'));Copy the code

2. Extends and Implement in interface intersects the types & in type

name: string; } interface User extends Name { age: number } let stu:User = {name: 'wang', age: 10} // Interface extension can be implemented with type cross (&) type Name = {Name: string; } type User= Name & {age: number} let stu:User={Name: 'wang', age: 18} //interface extension type type Name ={Name: string; } interface User extends Name { age: number; } let stu:User={name: 'wang', age: 89} } type User = Name & { age: number; } let stu:User={name:'wang', age: 18}Copy the code

3, There is a keyword in type can be mapped type, interface does not

Type DuKey = {[Key in Keys]: string // Similar for... In} let stu: Dukey = {name: 'wang', sex: 'man'}Copy the code