//2023/01/20
type Result = "pass" | "fail";
function verify(result: Result) {
if (result === "pass") {
console.log("Passed");
} else {
console.log("Failed");
}
}
//
//字符串
const str: string = "Hello world";
//数字
const num: number = 1;
//布尔
const bool: boolean = true;
//数组
const arr: number[] = [1, 2, 3];
// arr[0] ='0' //这样会报错
//对象
const obj: { x: number } = { x: 1, y: 2 }; //多了一个参数
//可选字段
const optionalObj: { x: number; y?: number } = { x: 1 }; //这样的话,一个也行,两个也行
optionalObj.y = "y"; //类型不对,得是数字类型
//对象类型分为 接口 和 类型别名
//接口
interface IType {
str: string;
}
//类型别名
type TType = {
str: string;
};
//类型推断,就是你没去定义它的类型,它会根据你的值的类型,给它加 类型
let n = "1 ";
//类型断言 , 就是 指定 对象里的某个值 的类型 ,然后就可以调用该类型的相关方法
console.log((x.contents as string).toLowerCase());
//泛型,包括泛型对象类型、泛型函数、泛型类
interface IGeneric<Type> {
test: Type;
}
//特殊类型: void、any、unknown、never
//any
const any: any = {};
ts学习。
最新推荐文章于 2024-07-21 23:55:11 发布