TypeScript中数组类型的定义⼀般数组类型的定义
// 类型推断
const numberArr = [1, 2, 3];
// 类型注释
const numberArr: number[] = [1, 2, 3];
const stringArr: string[] = ["a", "b", "c"];
const undefinedArr: undefined[] = [undefined, undefined];
// 存在多种类型
const arr: (number | string)[] = [1, "string", 2];
数组中对象类型的定义
const xiaoJieJies: { name: string, age: Number }[] = [
{ name: "刘英", age: 18 },
{ name: "谢⼤脚", age: 28 },
];
// 使⽤ type 类型别名定义
type Lady = { name: string, age: Number };
const xiaoJieJies: Lady[] = [
{ name: "刘英", age: 18 },
{ name: "谢⼤脚", age: 28 },
];
// 使⽤ class 类定义typescript 字符串转数组
class Madam {
name: string;
age: number;
}
const xiaoJieJies: Madam[] = [
{ name: "刘英", age: 18 },
{ name: "谢⼤脚", age: 28 },
];
参考:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论