实验室网站建设的调查报告,上海网络推广需要多少,在线网页制作,建设局长四、typscript(类型断言)-CSDN博客
5.1 ES 的内置对象(JS 中有很多内置对象#xff0c;可以直接在 TS 中当做定义好了的类型)
// Boolean、Error、Date、RegExp 等let b: Boolean new Boolean(1);
let e: Error new Error(Error occurred);
let d: Date new Date();
let …四、typscript(类型断言)-CSDN博客
5.1 ES 的内置对象(JS 中有很多内置对象可以直接在 TS 中当做定义好了的类型)
// Boolean、Error、Date、RegExp 等let b: Boolean new Boolean(1);
let e: Error new Error(Error occurred);
let d: Date new Date();
let r: RegExp /[a-z]/;5.2 DOM 和 BOM 的内置对象
// Document、HTMLElement、Event、NodeList 等let allDiv: NodeList document.querySelectorAll(div);
document.addEventListener(click, function(e: MouseEvent) {// Do something
});5.3 TS 核心库的定义文件
Math.pow(10, 2);
// 编译错误因为 Math.pow 必须接受两个 number 参数其类型定义如下
interface Math {pow(x: number, y: number): number;
}例2
document.addEventListener(click, function(e) {console.log(e.targetCurrent);
});
// 编译错误// addEventListener 在 TS 核心库的定义如下interface Document extends Node, GlobalEventHandlers, NodeSelector, DocumentEvent {addEventListener(type: string, listener: (ev: MouseEvent) any, useCapture?: boolean): void;
}
所以 e 被推断成了 MouseEvent而 MouseEvent 是没有 targetCurrent 属性的所以报错了(注意TS 核心库的定义中不包含 Node.js 部分。)