JSDoc 參考

以下清單概述了在 JavaScript 檔案中使用 JSDoc 註解提供類型資訊時,目前支援哪些建構。

請注意,任何未明確列在下方 (例如 @async) 的標籤目前都不支援。

類型

類別

文件

文件標籤在 TypeScript 和 JavaScript 中都能使用。

其他

意義通常與 jsdoc.app 中提供的標籤意義相同,或為其超集。下列程式碼說明了各標籤之間的差異,並提供每個標籤的一些範例用法。

注意:您可以使用 遊樂場來探索 JSDoc 支援

類型

@type

您可以使用「@type」標籤來參照類型。類型可以是

  1. 基本類型,例如 stringnumber
  2. 在 TypeScript 宣告中宣告,無論是全域或匯入。
  3. 在 JSDoc @typedef 標籤中宣告。

您可以使用大多數 JSDoc 類型語法和任何 TypeScript 語法,從 最基本的類型,例如 string最進階的類型,例如條件類型

js
/**
* @type {string}
*/
var s;
 
/** @type {Window} */
var win;
 
/** @type {PromiseLike<string>} */
var promisedString;
 
// You can specify an HTML Element with DOM properties
/** @type {HTMLElement} */
var myElement = document.querySelector(selector);
element.dataset.myData = "";
Try

@type 可以指定聯合類型 — 例如,某個項目可以是字串或布林值。

js
/**
* @type {string | boolean}
*/
var sb;
Try

您可以使用各種語法來指定陣列類型

js
/** @type {number[]} */
var ns;
/** @type {Array.<number>} */
var jsdoc;
/** @type {Array<number>} */
var nas;
Try

您也可以指定物件文字類型。例如,一個具有屬性「a」(字串)和「b」(數字)的物件會使用下列語法

js
/** @type {{ a: string, b: number }} */
var var9;
Try

您可以使用字串和數字索引簽章來指定類似映射和陣列的物件,並使用標準 JSDoc 語法或 TypeScript 語法。

js
/**
* A map-like object that maps arbitrary `string` properties to `number`s.
*
* @type {Object.<string, number>}
*/
var stringToNumber;
 
/** @type {Object.<number, object>} */
var arrayLike;
Try

前兩個類型等同於 TypeScript 類型 { [x: string]: number }{ [x: number]: any }。編譯器了解這兩種語法。

您可以使用 TypeScript 或 Google Closure 語法來指定函式類型

js
/** @type {function(string, boolean): number} Closure syntax */
var sbn;
/** @type {(s: string, b: boolean) => number} TypeScript syntax */
var sbn2;
Try

或者,您也可以只使用未指定的 Function 類型

js
/** @type {Function} */
var fn7;
/** @type {function} */
var fn6;
Try

Closure 中的其他類型也可以使用

js
/**
* @type {*} - can be 'any' type
*/
var star;
/**
* @type {?} - unknown type (same as 'any')
*/
var question;
Try

轉型

TypeScript 從 Google Closure 借用 cast 語法。這讓你可以透過在任何括號表達式之前加上 @type 標籤,將類型 cast 成其他類型。

js
/**
* @type {number | string}
*/
var numberOrString = Math.random() < 0.5 ? "hello" : 100;
var typeAssertedNumber = /** @type {number} */ (numberOrString);
Try

你甚至可以像 TypeScript 一樣 cast 成 const

js
let one = /** @type {const} */(1);
Try

匯入類型

你可以使用匯入類型,從其他檔案匯入宣告。此語法是 TypeScript 特有的,與 JSDoc 標準不同

js
// @filename: types.d.ts
export type Pet = {
name: string,
};
 
// @filename: main.js
/**
* @param {import("./types").Pet} p
*/
function walk(p) {
console.log(`Walking ${p.name}...`);
}
Try

匯入類型可以用在類型別名宣告中

js
/**
* @typedef {import("./types").Pet} Pet
*/
 
/**
* @type {Pet}
*/
var myPet;
myPet.name;
Try

匯入類型可以用來取得模組中值的類型,如果你不知道類型,或是有個大類型,輸入起來很麻煩

js
/**
* @type {typeof import("./accounts").userAccount}
*/
var x = require("./accounts").userAccount;
Try

@param@returns

@param 使用與 @type 相同的類型語法,但會加上一個參數名稱。參數也可以透過將名稱用方括弧包起來,宣告為可選的

js
// Parameters may be declared in a variety of syntactic forms
/**
* @param {string} p1 - A string param.
* @param {string=} p2 - An optional param (Google Closure syntax)
* @param {string} [p3] - Another optional param (JSDoc syntax).
* @param {string} [p4="test"] - An optional param with a default value
* @returns {string} This is the result
*/
function stringsStringStrings(p1, p2, p3, p4) {
// TODO
}
Try

同樣地,對於函式的回傳類型

js
/**
* @return {PromiseLike<string>}
*/
function ps() {}
 
/**
* @returns {{ a: string, b: number }} - May use '@returns' as well as '@return'
*/
function ab() {}
Try

@typedef@callback@param

你可以使用 @typedef 定義複雜類型。類似的語法適用於 @param

js
/**
* @typedef {Object} SpecialType - creates a new type named 'SpecialType'
* @property {string} prop1 - a string property of SpecialType
* @property {number} prop2 - a number property of SpecialType
* @property {number=} prop3 - an optional number property of SpecialType
* @prop {number} [prop4] - an optional number property of SpecialType
* @prop {number} [prop5=42] - an optional number property of SpecialType with default
*/
 
/** @type {SpecialType} */
var specialTypeObject;
specialTypeObject.prop3;
Try

你可以在第一行使用 objectObject

js
/**
* @typedef {object} SpecialType1 - creates a new type named 'SpecialType1'
* @property {string} prop1 - a string property of SpecialType1
* @property {number} prop2 - a number property of SpecialType1
* @property {number=} prop3 - an optional number property of SpecialType1
*/
 
/** @type {SpecialType1} */
var specialTypeObject1;
Try

@param 允許類似的語法,用於一次性的類型規格。請注意,巢狀屬性名稱必須加上參數名稱為前綴

js
/**
* @param {Object} options - The shape is the same as SpecialType above
* @param {string} options.prop1
* @param {number} options.prop2
* @param {number=} options.prop3
* @param {number} [options.prop4]
* @param {number} [options.prop5=42]
*/
function special(options) {
return (options.prop4 || 1001) + options.prop5;
}
Try

@callback 類似於 @typedef,但它指定函式類型,而非物件類型

js
/**
* @callback Predicate
* @param {string} data
* @param {number} [index]
* @returns {boolean}
*/
 
/** @type {Predicate} */
const ok = (s) => !(s.length % 2);
Try

當然,這些類型可以使用單行 @typedef 中的 TypeScript 語法宣告

js
/** @typedef {{ prop1: string, prop2: string, prop3?: number }} SpecialType */
/** @typedef {(data: string, index?: number) => boolean} Predicate */

@template

您可以使用 @template 標籤宣告類型參數。這讓您可以建立泛型的函式、類別或類型

js
/**
* @template T
* @param {T} x - A generic parameter that flows through to the return type
* @returns {T}
*/
function id(x) {
return x;
}
 
const a = id("string");
const b = id(123);
const c = id({});
Try

使用逗號或多個標籤宣告多個類型參數

js
/**
* @template T,U,V
* @template W,X
*/

您也可以在類型參數名稱之前指定類型約束。清單中的第一個類型參數會受到約束

js
/**
* @template {string} K - K must be a string or string literal
* @template {{ serious(): string }} Seriousalizable - must have a serious method
* @param {K} key
* @param {Seriousalizable} object
*/
function seriousalize(key, object) {
// ????
}
Try

最後,您可以為類型參數指定預設值

js
/** @template [T=object] */
class Cache {
/** @param {T} initial */
constructor(initial) {
}
}
let c = new Cache()
Try

@satisfies

@satisfies 提供存取 TypeScript 中後綴 運算子 satisfies 的功能。Satisfies 用於宣告值實作類型,但不會影響該值的類型。

js
// @ts-check
/**
* @typedef {"hello world" | "Hello, world"} WelcomeMessage
*/
 
/** @satisfies {WelcomeMessage} */
const message = "hello world"
const message: "hello world"
 
/** @satisfies {WelcomeMessage} */
Type '"Hello world!"' does not satisfy the expected type 'WelcomeMessage'.1360Type '"Hello world!"' does not satisfy the expected type 'WelcomeMessage'.
const failingMessage = "Hello world!"
 
/** @type {WelcomeMessage} */
const messageUsingType = "hello world"
const messageUsingType: WelcomeMessage
Try

類別

類別可以宣告為 ES6 類別。

js
class C {
/**
* @param {number} data
*/
constructor(data) {
// property types can be inferred
this.name = "foo";
 
// or set explicitly
/** @type {string | null} */
this.title = null;
 
// or simply annotated, if they're set elsewhere
/** @type {number} */
this.size;
 
this.initialize(data); // Should error, initializer expects a string
}
/**
* @param {string} s
*/
initialize = function (s) {
this.size = s.length;
};
}
 
var c = new C(0);
 
// C should only be called with new, but
// because it is JavaScript, this is allowed and
// considered an 'any'.
var result = C(1);
Try

它們也可以宣告為建構函式;為此,請使用 @constructor@this

屬性修飾詞

@public@private@protected 的作用與 TypeScript 中的 publicprivateprotected 完全相同

js
// @ts-check
 
class Car {
constructor() {
/** @private */
this.identifier = 100;
}
 
printIdentifier() {
console.log(this.identifier);
}
}
 
const c = new Car();
console.log(c.identifier);
Property 'identifier' is private and only accessible within class 'Car'.2341Property 'identifier' is private and only accessible within class 'Car'.
Try
  • @public 始終是隱含的,可以省略,但表示可以從任何地方存取屬性。
  • @private 表示只能在包含類別中使用屬性。
  • @protected 表示只能在包含類別中使用屬性,以及所有衍生子類別,但不能在包含類別的不同實例中使用。

@public@private@protected 在建構函式中不起作用。

@readonly

@readonly 修飾詞可確保屬性僅在初始化期間寫入。

js
// @ts-check
 
class Car {
constructor() {
/** @readonly */
this.identifier = 100;
}
 
printIdentifier() {
console.log(this.identifier);
}
}
 
const c = new Car();
console.log(c.identifier);
Try

@override

@override 的作用與 TypeScript 中相同;將其用於覆寫基底類別中方法的方法

js
export class C {
m() { }
}
class D extends C {
/** @override */
m() { }
}
Try

在 tsconfig 中設定 noImplicitOverride: true 以檢查覆寫。

@extends

當 JavaScript 類別延伸泛型基底類別時,JavaScript 語法中沒有傳遞型別參數。@extends 標籤允許這樣做

js
/**
* @template T
* @extends {Set<T>}
*/
class SortableSet extends Set {
// ...
}
Try

請注意,@extends 僅適用於類別。目前,建構函式無法延伸類別。

@implements

同樣地,TypeScript 介面沒有 JavaScript 語法可以實作。@implements 標籤的作用與 TypeScript 中相同

js
/** @implements {Print} */
class TextBook {
print() {
// TODO
}
}
Try

@constructor

編譯器會根據 this 屬性指定推論建構函式,但如果你加入 @constructor 標籤,可以讓檢查更嚴格,建議更好

js
/**
* @constructor
* @param {number} data
*/
function C(data) {
// property types can be inferred
this.name = "foo";
 
// or set explicitly
/** @type {string | null} */
this.title = null;
 
// or simply annotated, if they're set elsewhere
/** @type {number} */
this.size;
 
this.initialize(data);
Argument of type 'number' is not assignable to parameter of type 'string'.2345Argument of type 'number' is not assignable to parameter of type 'string'.
}
/**
* @param {string} s
*/
C.prototype.initialize = function (s) {
this.size = s.length;
};
 
var c = new C(0);
c.size;
 
var result = C(1);
Value of type 'typeof C' is not callable. Did you mean to include 'new'?2348Value of type 'typeof C' is not callable. Did you mean to include 'new'?
Try

注意:錯誤訊息只會出現在啟用 JSConfigcheckJs 的 JS 程式碼庫中。

使用 @constructor 時,this 會在建構函式 C 中檢查,因此你會取得 initialize 方法的建議,如果你傳遞數字,則會出現錯誤。如果你呼叫 C 而不是建構它,編輯器也可能會顯示警告。

遺憾的是,這表示同時也可以呼叫的建構函式無法使用 @constructor

@this

當編譯器有一些背景可以運作時,通常可以找出 this 的類型。當它沒有時,你可以使用 @this 明確指定 this 的類型

js
/**
* @this {HTMLElement}
* @param {*} e
*/
function callbackForLater(e) {
this.clientHeight = parseInt(e); // should be fine!
}
Try

文件

@deprecated

當函式、方法或屬性已棄用時,你可以透過標記 /** @deprecated */ JSDoc 註解來讓使用者知道。該資訊會顯示在完成清單中,並作為編輯器可以特別處理的建議診斷。在類似 VS Code 的編輯器中,已棄用的值通常會以刪除線樣式顯示 像這樣

js
/** @deprecated */
const apiV1 = {};
const apiV2 = {};
 
apiV;
   
 
 
Try

@see

@see 讓你連結到程式中的其他名稱

ts
type Box<T> = { t: T }
/** @see Box for implementation details */
type Boxify<T> = { [K in keyof T]: Box<T> };
Try

有些編輯器會將 Box 變成連結,讓你可以輕鬆地前往並返回。

@link 類似於 @see,但它可以用於其他標籤中

ts
type Box<T> = { t: T }
/** @returns A {@link Box} containing the parameter. */
function box<U>(u: U): Box<U> {
return { t: u };
}
Try

其他

@enum

@enum 標籤可讓您建立物件文字,其成員皆為指定類型。與 JavaScript 中的大多數物件文字不同,它不允許其他成員。@enum 的用意是與 Google Closure 的 @enum 標籤相容。

js
/** @enum {number} */
const JSDocState = {
BeginningOfLine: 0,
SawAsterisk: 1,
SavingComments: 2,
};
 
JSDocState.SawAsterisk;
Try

請注意,@enum 與 TypeScript 的 enum 非常不同,而且簡單許多。但是,與 TypeScript 的列舉不同,@enum 可以有任意類型

js
/** @enum {function(number): number} */
const MathFuncs = {
add1: (n) => n + 1,
id: (n) => -n,
sub1: (n) => n - 1,
};
 
MathFuncs.add1;
Try

@author

您可以使用 @author 指定項目的作者

ts
/**
* Welcome to awesome.ts
* @author Ian Awesome <i.am.awesome@example.com>
*/
Try

請記得用尖括號括住電子郵件地址。否則,@example 會被解析為新標籤。

其他支援的模式

js
var someObj = {
/**
* @param {string} param1 - JSDocs on property assignments work
*/
x: function (param1) {},
};
 
/**
* As do jsdocs 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 function expressions
* @param {number} x - A multiplier
*/
let myArrow = (x) => x * x;
 
/**
* Which means it works for function components in JSX too
* @param {{a: string, b: number}} props - Some param
*/
var fc = (props) => <div>{props.a.charAt(0)}</div>;
 
/**
* A parameter can be a class constructor, using Google 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();
}
Try

不支援的模式

物件文字類型中屬性類型上的後置等於號並未指定選用屬性

js
/**
* @type {{ a: string, b: number= }}
*/
var wrong;
/**
* Use postfix question on the property name instead:
* @type {{ a: string, b?: number }}
*/
var right;
Try

只有在 strictNullChecks 開啟時,可為空類型才有意義

js
/**
* @type {?number}
* With strictNullChecks: true -- number | null
* With strictNullChecks: false -- number
*/
var nullable;
Try

TypeScript 原生語法是聯集類型

js
/**
* @type {number | null}
* With strictNullChecks: true -- number | null
* With strictNullChecks: false -- number
*/
var unionNullable;
Try

不可為空類型沒有意義,且只會當成其原始類型處理

js
/**
* @type {!number}
* Just has type number
*/
var normal;
Try

與 JSDoc 的類型系統不同,TypeScript 只允許您將類型標記為包含 null 或不包含 null。沒有明確的不可為空性 — 如果 strictNullChecks 開啟,則 number 不可為空。如果關閉,則 number 可為空。

不支援的標籤

TypeScript 會忽略任何不支援的 JSDoc 標籤。

下列標籤有公開議題支援

TypeScript 文件是一個開放原始碼專案。請協助我們改善這些頁面 傳送 Pull Request

此頁面的貢獻者
OTOrta Therox (23)
NSNathan Shively-Sanders (6)
RGRohit Gohri (2)
GUGreg Uzelac (1)
D(KDylan (Jeongtae Kim) (1)
6+

最後更新時間:2024 年 3 月 21 日