Typescript入门-数据类型与语法

let e:unknown;
// unkonwn 
// 必须使用 类型断言缩小未知范围 才能被赋值
// 或
// 使用类型收缩 如 typeof 或 instanceof
// https://blog.csdn.net/azl397985856/article/details/104470902
e = 'hello';
let s:string;
// 断言
s = e as string;
s = <string>e;

//never 无返回值
function fn2(): never{
    throw new Error("报错了!");
}
// {指定对象内包含哪些属性} ?: 可选 
let b: {name: string, age?: number, [propName: string]: any};
b = {name: "mike", "gender": 18}

let f: (a: number, b: number)=>number;
f = function (n1: number, n2: number): number{
    return n1 + n2;
}

let arr: string[];
// let arr: Array<string>;
// 元组 固定长度数组
let h: [string, string];
h = ['hello', 'world'];

// enum 枚举
enum Gender{
    Male,
    Female
}

let i: {gender: Gender};

i = {
    gender: Gender.Male
}

// &表示同时
let j: string & number;
// 
let j1: { name: string } & { age: number };
j1 = {name: "mike", age: 18};

// 类型别名
let k: 1 | 2 | 3 | 4 | 5;
k = 5;
type myType = string;
let m:myType;

  

Typescript入门-数据类型与语法 x
上一篇:macbook安装typescript环境added 1 package, and audited 2 packages in 26sfound 0 vulnerabilities代表成功了


下一篇:webstorm激活码webstorm激活码最新2021专业版本永久有效