TypeScript语法提要
2018-09-22 本文已影响13人
Jelif
背景
- TypeScript是一种类似于JavaScript的语言,为JavaScript加上了类型(Type)特性。
- TypeScript由微软开发,谷歌支持(Angular2框架的开发语言)。
- 在线代码编辑地址。
类型
var str: string;
function func(param:string): number{
return 0;
}
var str;
function func(param) {
return 0;
}
`式字符串
多行
var str = `wrewrew
wqewerew
`;
var str = "wrewrew\nwqewerew\n";
参数化
var x = "xxx";
var y = "yyy";
var z = `${x}${y}`
var x = "xxx";
var y = "yyy";
var z = "" + x + y;
函数
默认值、可选值
function func(param1, param2: string = "", param3?) {}
function func(param1, param2, param3) {
if (param2 === void 0) { param2 = ""; }
}
不定长参数
生成器
箭头式函数
析构表达式
面向对象
接口、类、继承、构造器、访问修饰符
interface IPerson{
getName();
}
class Person implements IPerson{
constructor(private name) { }
public getName() {
return this.name;
}
}
class Employee extends Person{
constructor(name, private hiredate) {
super(name);
}
}
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Person = /** @class */ (function () {
function Person(name) {
this.name = name;
}
Person.prototype.getName = function () {
return this.name;
};
return Person;
}());
var Employee = /** @class */ (function (_super) {
__extends(Employee, _super);
function Employee(name, hiredate) {
var _this = _super.call(this, name) || this;
_this.hiredate = hiredate;
return _this;
}
return Employee;
}(Person));
范型
模块
注解
类型定义文件