1、适合新手入门;
2、眼过千遍,不如手过一遍,看文档,我忘记了很多次;建议大家边看边运行Demo。
3、官网在线运行地址:https://www.typescriptlang.org/play
使用ts-node直接运行TypeScript代码
npm install -g ts-node ts-node demo1.ts
即函数的返回值类型
function sanitizeInput(str: string): string { return str + ',arden'; } console.log(sanitizeInput('hi'));
都可以声明类型
interface Point { x: number; y: number; } //type Point { // x: number; // y: number; //} function printCoord(pt: Point) { console.log("The coordinate's x value is " + pt.x); console.log("The coordinate's y value is " + pt.y); } printCoord({ x: 100, y: 200 });
类型 | 拓展方式 | 表列 B |
---|---|---|
interface | 通过extends 父类型拓展 | 可以向现有类型添加新字段 |
type | 通过 父类型 &{} 拓展 | 不可以向现有界面添加新字段 |
https://www.typescriptlang.org