声明数组的两种方式
1.1 基本的
let arr:number[] =[1,2,3]
1.2结合泛型
let arr:{test:string}[] =[{test:‘1’}]
let arr:Array<{test:string}>=[{test:‘1’}]
声明对象的两种方式
let obj1:object ={test:‘1’}
let obj1:{test:string} ={test:‘1’}
3.接口interface
interface:接口
概念:定义参数或方法的数据类型;
interface fa {
test: string;
}
使用
const obj: fa = { test: “123” };
枚举enum
enum fn {
fn1 = “fn1”,
fn2 = “fn2”
}
enum fn2 {
fn1,
fn2
}
console.log(fn.fn1, fn.fn2);
// fn1",“fn2”
console.log(fn2.fn1, fn2.fn2);
//0 1
type
type和interface都可以定义参数或方法的数据类型;
定义方式也有不同 多“=”
type fa = {
test: string;
};
使用
const obj: fa = { test: “123” };
但也有区别的,type用于其它类型 (联合类型、元组类型、基本类型(原始值)),interface不支持
例如:
type PartialPointX = { x: number };
type PartialPointY = { y: number };
// union(联合)
type PartialPoint = PartialPointX | PartialPointY;
这个时候就不能使用interface了
又例如:
interface Response<Result = any> {
result?: Result;
errorCode?: string;
}
type CheckCrowdAccessType = Response<{ iCrowd: ICrowd }>;
//这个时候也是不能用interface的 因为结合了之前定义的数据类型Response了