ts定义数组
// 定义整数型数组 const arr1: number[] = [] const arr1_1: Array<number> = [] // 定义字符窜型数组 const arr2: string[] = [] const arr2_1: Array<string> = [] // 定义对象型数组 const arr3: object[] = [] const arr3_1: Array<Object> = [] const arr3_2: Array<{ title: string; show: boolean; num?: number }> = [] // 定义任意类型数组 const arr4: any[] = [] const arr4_1: Array<any> = [] //定义二维数组类型(n维数组类似) const arr5:Array<Array<any>> = []
如果类型比较多推荐使用接口去写
interface Config { name: string; value: string; icon: string; occupancy?: number; detail?: string; } //const configs: Config[] =[] const configs: Config[] = reactive([ { name: '内存', value: '', icon: memoryIcon, }, { name: 'CPU', value: '', icon: cpuIcon, }, { name: 'GPU', value: '', icon: gpuIcon, }, { name: '数据盘硬盘使用量', value: '', occupancy: 0, icon: diskIcon, }, ]);
ts声明函数类型
// 语法一 function fn(param1:number,param2:string):string{ if (param1 === 1){ return param2 }else { return "hello world" } } // 语法2 let fn2:(param1:number,param2:string) => string fn2 = function(param1,param2){ if (param1 === 1){ return param2 }else { return "hello world" } }
标签:const,string,ts,TS,param1,数组,Array,param2 From: https://www.cnblogs.com/zcm1688/p/18297969