首页 > 其他 > 详细

TypeScript - 内置对象

时间:2019-05-17 17:37:00      阅读:112      评论:0      收藏:0      [点我收藏+]

JavaScript 中有很多内置对象,它们可以直接在 TypeScript 中当做定义好了的类型。内置对象是指根据标准在全局作用域(Global)上存在的对象。这里的标准是指 ECMAScript 和其他环境(比如 DOM)的标准。

ECMAScript 标准提供的内置对象有:BooleanErrorDateRegExp 等。我们可以在 TypeScript 中将变量定义为这些类型:

let b: Boolean = new Boolean(1);
let e: Error = new Error(Error occurred);
let d: Date = new Date();
let r: RegExp = /[a-z]/;

DOM 和 BOM 提供的内置对象有:DocumentHTMLElementEventNodeList 等。

let body: HTMLElement = document.body;
let allDiv: NodeList = document.querySelectorAll(div);
document.addEventListener(click, function(e: MouseEvent) {
    // Do something
});

当你在使用一些常用的方法的时候,TypeScript 实际上已经帮你做了很多类型判断的工作了,比如:

Math.pow(10, 2);
 // index.ts(1,14): error TS2345: Argument of type ‘string‘ is not assignable to parameter of type ‘number‘.

上面的例子中,Math.pow 必须接受两个 number 类型的参数。事实上 Math.pow 的类型定义如下:

interface Math {
/**
* Returns the value of a base expression taken to a specified power.
* @param x The base value of the expression.
* @param y The exponent value of the expression.
*/
pow(x: number, y: number): number;
}

再举一个 DOM 中的例子:

document.addEventListener(click, function(e) {
    console.log(e.targetCurrent);
});
 
// index.ts(2,17): error TS2339: Property ‘targetCurrent‘ does not exist on type ‘MouseEvent‘.

上面的例子中,addEventListener 方法是在 TypeScript 核心库中定义的:

interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEvent {
  addEventListener(type: string, listener: (ev: MouseEvent) => any, useCapture?: boolean): void;
}

所以 e 被推断成了 MouseEvent,而 MouseEvent 是没有 targetCurrent 属性的,所以报错了。

注意,TypeScript 核心库的定义中不包含 Node.js 部分。用 TypeScript 写 Node.js,Node.js 不是内置对象的一部分,如果想用 TypeScript 写 Node.js,则需要引入第三方声明文件:npm install @types/node --save-dev

TypeScript - 内置对象

原文:https://www.cnblogs.com/xjy20170907/p/10882509.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!