TypeScript基礎入門之Javascript檔案型別檢查(五)
阿新 • • 發佈:2018-12-18
@constructor
編譯器根據此屬性賦值推斷建構函式,但如果新增@constructor標記,則可以更好地檢查更嚴格和更好的建議:
/** * @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而不是構造它,您也會收到錯誤。
不幸的是,這意味著也可以呼叫的建構函式不能使用@constructor。
@this
當編譯器有一些上下文可以使用時,它通常可以找出它的型別。 如果沒有,您可以使用@this顯式指定此型別:
/** * @this {HTMLElement} * @param {*} e */ function callbackForLater(e) { this.clientHeight = parseInt(e) // should be fine! }
@extends
當Javascript類擴充套件通用基類時,無處可指定型別引數應該是什麼。 @extends標記為該型別引數提供了一個位置:
/**
* @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可以為空。