区别于 js 的动态类型
TS 适用于一些中大型项目,使其变得便于维护 JS 更适合一些简单的项目,可以快速完成 TS 可以提前避免更多错误
type Employee = { readonly id: number; name: string; retire: (date: Date) => void; }; let employee: Employee = { id: 1, name: "rabithua", retire: (date: Date) => { console.log(date); }, };
function kg2Lbs(weight: number | string): number { if (typeof weight === "number") { return weight * 2.2; } else return parseInt(weight) * 2.2; } kg2Lbs(10); kg2Lbs("10");
type Draggable = { drag: () => void; }; type Resizeable = { resize: () => void; }; type UIWidget = Draggable & Resizeable; let textBox: UIWidget = { drag: () => {}, resize: () => {}, };
type num = 1 | 2; type str = "1" | "2";
本站由 于长野 提供服务