TypeScript 版本相关
TypeScript 1.3
元组类型
// Declare a tuple type
var x: [string, number];
// 初始化
x = ["hello", ]; // ok
// 错误的初始化
x = [, "hello"]; // Error
TypeScript 1.4
let 声明
在JavaScript里, var声明会被"提升"到所在作用域的顶端,这可能会引发一些让人不解的bugs: console.log(x); // meant to write 'y' here
/* later in the same block */
var x = "hello"; TypeScript已经支持新的ES6的关键字let, 声明一个块级作用域的变量.一个let变量只能在声明之后的位置被引用,并且作用域为声明它的块里: if (foo) {
console.log(x); // Error, cannot refer to x before its declaration
let x = "hello";
} else {
console.log(x); // Error, x is not declared in this block
}
const 声明
const halfPi = Math.PI / ;
halfPi = ; // Error, cant' assign to a 'const'
类型别名
type PrimitiveArray = Array<string|number|boolean>;
type MyNumber = number;
type NgScope = ng.IScope;
type Callback = () => void;
const enum(完全嵌入的枚举)
const enum Suit { Clubs, Diamonds, Hearts, Spades }
var d = Suit.Diamonds;
Compiles to exactly;
var d = ;
Typescript 1.5
导出声明
interface Stream { ... }
function writeToStream(stream: Stream, data: string) { ... }
export { Stream, writeToStream as write }; // writeToStream 导出为 write
import { read, write, standardOutput as stdout } from "./inout";
var s = read(stdout);
write(stdout, s);
import * as io from "./inout";
var s = io.read(io.standardOutput);
io.write(io.standardOutput, s);
重新导出
export { read, write, standardOutput as stdout } from "./inout";
export function transform(s: string): string { ... }
export * from "./mod1";
export * from "./mod2";
默认导出项
export default class Greeter {
sayHello() {
console.log("Greeting!");
}
}
import Greeter from "./greeter";
var g = new Greeter();
g.sayHello();
无导入加载
import "./polyfills"
TypeScript 1.6
交叉类型(intersection types)
Typescript 1.6 引入了交叉类型作为联合类型(union types)逻辑上的补充,联合类型 A | B 表示一个类型为A或B的实体, 而交叉类型 A & B 表示一个类型同时为 A 或 B的实体
function extend<T, U>(first: T, second: U): T & U {
let result = <T & U> {};
for (let id in first) {
result[id] = first[id];
}
for (let id in second) {
if (!result.hasOwnProperty(id)) {
result[id] = second[id];
}
}
return result;
}
var x = extend({ a: "hello" }, { b: });
var s = x.a;
var n = x.b;
type LinkedList<T> = T & { next: LinkedList<T> };
interface Person {
name: string;
}
var people: LinkedList<Person>;
var s = people.name;
var s = people.next.naem;
var s = people.next.next.name;
var s = people.next.next.next.name;
interface A { a: string }
interface B { b: string }
interface C { c: string }
var abc: A & B & C;
abc.a = "hello";
abc.b = "hello";
abc.c = "hello";
类表达式
TypeScript 1.6 增加了对ES6类表达式的支持. 在一个类表达式中, 类的名称是可选的, 如果指明, 作用域仅限于类表达式本身. 这和函数表达式可选的名称类似. 在类表达式外无法引用其实例类型. 但是自然也能够从类型结构上匹配.
let Point = class {
constructor(public x: number, public y: number) { }
public length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
};
var p = new Point(, );
console.log(p.length());
继承表达式
// 继承内建类
class MyArray extends Array<number> { }
class MyError extends Error { } // 继承表达式
class ThingA {
getGreeting() { return "Hello from A"; }
} class ThingB {
getGreeting() { return "Hello from B"; }
} interface Greeter {
getGreeting(): string;
} interface GreeterConstructor() {
new (): Greeter;
} function getGreeterBase(): GreeterConstructor {
return Math.random() >= 0.5 ? ThingA : ThingB;
} class Test extends getGreeterBase() {
sayHello() {
console.log(this.getGreeting());
}
}
TypeScript 1.7
TypeScript 1.8
隐式返回
function f(x) { // 错误: 不是所有分支都返回了值
if (x) {
return false;
}
// 隐式返回了 "undefined"
}
TypeScript 版本相关的更多相关文章
- 查询ubuntu系统版本相关信息
查询ubuntu系统版本相关信息 sky@sky-virtual-machine:~$ cat /etc/issueUbuntu 12.04.5 LTS \n \l proc目录下记录的当前系统运行的 ...
- electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint
我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...
- 查看linux版本相关命令
Linux系统中,平常要装软件需要知道该系统相关信息,特别是刚刚进入公司,要接手某个系统,需要配置相关服务的时候,必须要先知道该系统的一些信息包括:内核版本信息.发行版本信息. #lsb_releas ...
- Vue练手项目(包含typescript版本)
本项目的git仓库https://github.com/lznism/xiachufang-vue 对应的使用typescript实现的版本地址https://github.com/lznism/xi ...
- 关于.NET Core 2.0.2升级到2.1.1版本相关问题
之前,因日常任务管理比较混乱,所以自己开发了PTager任务管理系统. 当时用了.NET Core 2.0版本. 现在想修改相关功能,但.NET Core已发布到2.1.301了,也即2.1.1. 附 ...
- Visual Studio 2017RC 版本相关资料
Visual Studio 2017 RC版本说明 1.社区版 Visual Studio Community 2017 RC Visual Studio Community 2017 RC 是针对个 ...
- jdk版本相关问题
1.switch在jdk1.7版本之后开始支持String类型: 2.maven3版本默认支持jdk版本为jdk1.5 3.编辑器中jdk版本设置为1.7或1.8版本,但未指定maven中的jdk版本 ...
- 【android】安卓的权限提示及版本相关
Only dangerous permissions require user agreement. The way Android asks the user to grant dangerous ...
- 自定义npm包——typeScript版本
前言 这篇文章是在我之前的文章 [自定义npm包的创建.发布.更新和撤销] 的基础上做的扩展,主要是针对如何创建以及发布一个typeScript语言的npm包. 大纲 1.创建关于typeScript ...
随机推荐
- 背景图片利用backgrond-posintion属性实现不同形式的分割
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Android 面试100问- 0序0
准备找android方面的工作,现收集面试题,打算收集100个并记录
- nodejs-POST数据处理
GET数据:容量小 32K 数据在URL中 POST数据:数据量大 1G 分段传输 数据另外发 处理方法: const http=require("http"); http.cre ...
- node.js安装使用express框架
官网:http://www.expressjs.com.cn/ 使用方式(如果后面需要添加路由等推荐第二种方式,不然需要需要手动添加):一.自己动手搭建 1. 新建项目文件夹,如test,并在命令行中 ...
- render函数(转)
https://blog.csdn.net/qq78827534/article/details/80792514
- sql注入1
一.函数 1.version() MYsql版本 2.user() 数据库用户名 3.database() 数据库名 4.@@datadir 数据库路径 5.@@version_compi ...
- springboot application.properties 常用完整版配置信息
从springboot官方文档中扒出来的,留存一下以后应该会用到 # ================================================================= ...
- git教程:管理修改
转载:管理修改 现在,假定你已经完全掌握了暂存区的概念.下面,我们要讨论的就是,为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件. 你会问,什么是修改?比如你新增了一行 ...
- React Native 获取组件(Component)在屏幕上的位置
年后主客户端的需求以及老的业务迁移RN,现在疯狂的在学RN.在迁移需求的时候遇到需要获取组件在屏幕上的绝对位置.页面如下: 就需要展开的时候获取sectionHeader(默认排序)在屏幕上的具体位置 ...
- JDK源码看ArrayList和Vector的一些区别
最近在看JDK源码,从源码的角度记录一下ArrayList和Vector的一些区别 1.new a.不指定长度 Vector默认创建10个元素的数组 public Vector() { this(10 ...