转发 TypeScript基础入门之Javascript文件类型检查(五)express
继续上篇文章【TypeScript基础入门之Javascript文件类型检查(四)】less
编译器根据此属性赋值推断构造函数,但若是添加@constructor标记,则能够更好地检查更严格和更好的建议:post
/** * @constructor * @param {number} data */ function C(data) { this.size = 0; this.initialize(data); // Should error, initializer expects a string } /** * @param {string} s */ C.prototype.initialize = function (s) { this.size = s.length } var c = new C(0); var result = C(1); // C should only be called with new
使用@constructor,在构造函数C中检查它,所以您将得到初始化方法的建议,若是您传递一个数字,则会出错。 若是您调用C而不是构造它,您也会收到错误。this
不幸的是,这意味着也能够调用的构造函数不能使用@constructor。spa
@this.net
当编译器有一些上下文能够使用时,它一般能够找出它的类型。 若是没有,您能够使用@this显式指定此类型:prototype
/** * @this {HTMLElement} * @param {*} e */ function callbackForLater(e) { this.clientHeight = parseInt(e) // should be fine! }
@extendsrest
当Javascript类扩展通用基类时,无处可指定类型参数应该是什么。 @extends标记为该类型参数提供了一个位置:code
/** * @template T * @extends {Set<T>} */ class SortableSet extends Set { // ... }
请注意,@ extends仅适用于类。 目前,构造函数没有办法扩展一个类。
@enum
@enum标记容许您建立一个对象文字,其成员都是指定的类型。 与Javascript中的大多数对象文字不一样,它不容许其余成员。
/** @enum {number} */ const JSDocState = { BeginningOfLine: 0, SawAsterisk: 1, SavingComments: 2, }
请注意,@enum与Typescript的枚举彻底不一样,而且简单得多。 可是,与Typescript的枚举不一样,@enum能够有任何类型:
/** @enum {function(number): number} */ const Math = { add1: n => n + 1, id: n => -n, sub1: n => n - 1, }
更多示例
var someObj = { /** * @param {string} param1 - Docs on property assignments work */ x: function(param1){} }; /** * As do docs on variable assignments * @return {Window} */ let someFunc = function(){}; /** * And class methods * @param {string} greeting The greeting to use */ Foo.prototype.sayHi = (greeting) => console.log("Hi!"); /** * And arrow functions expressions * @param {number} x - A multiplier */ let myArrow = x => x * x; /** * Which means it works for stateless function components in JSX too * @param {{a: string, b: number}} test - Some param */ var sfc = (test) => <div>{test.a.charAt(0)}</div>; /** * A parameter can be a class constructor, using Closure syntax. * * @param {{new(...args: any[]): object}} C - The class to register */ function registerClass(C) {} /** * @param {...string} p1 - A 'rest' arg (array) of strings. (treated as 'any') */ function fn10(p1){} /** * @param {...string} p1 - A 'rest' arg (array) of strings. (treated as 'any') */ function fn9(p1) { return p1.join(); }
引用值空间中的对象,由于类型不起做用,除非对象也建立类型,如构造函数。
function aNormalFunction() { } /** * @type {aNormalFunction} */ var wrong; /** * Use 'typeof' instead: * @type {typeof aNormalFunction} */ var right;
对象文字类型中的属性类型的Postfix等于未指定可选属性:
/** * @type {{ a: string, b: number= }} */ var wrong; /** * Use postfix question on the property name instead: * @type {{ a: string, b?: number }} */ var right;
若是启用了strictNullChecks,则可空类型仅具备意义:
/** * @type {?number} * With strictNullChecks: true -- number | null * With strictNullChecks: off -- number */ var nullable;
非可空类型没有任何意义,而且被视为原始类型:
/** * @type {!number} * Just has type number */ var normal;
与JSDoc的类型系统不一样,Typescript只容许您将类型标记为包含null或不包含null。 没有明确的非可空性 - 若是启用了strictNullChecks,则number不可为空。 若是它关闭,则number能够为空。