1.npm install -g typescript ---安装一个全局的TypeScript

2.创建第一个TypeScript文件,

3.用命令行 tsc greeter.ts 打包文件。
这个命令行其实就是将 greeter.ts 打包成greeter.js 。将TypeScript 的代码,打包成JavaScript 的代码。

4.添加给 person 添加注解,如下图,其实就是约束person 是string 类型的,传进来别的类型的时候,在使用 tsc 命令行的时候 会报错。

5.TypeScript 接口

6.TypeScript 类
类和接口的良好配合使用,决定一个程序员的抽象水平。

在构造函数中,例如public firstName, 其实相当于在外面定义了 firstName 属性,首页user 这个对象其实是有4个属性的,分别是 fullName,firstName,middleInitial,lastName
我们可以看看 打包都生成的js文件:

7.运行html页面: 在html页面 是引入 greeter.js 文件,例如

网友评论