TypeScript - 如何記錄泛型型別參數?
假設下面有一個通用類型,我應該使用什麼來取代 ??? 以正確地記錄 T?
/**
* ??? The description of T.
*/
class MyClass<T> {
}
在 C# 中,我會使用 <typeparam>。有官方的 JSDoc 對應嗎?
答案#1
VLAZ 在評論中指出,@template 可以使用,但並未在官方 JSDoc 文件中提及。然而,它在 TypeScript 自身的 JSDoc 參考中有提到:
你可以使用
@template標籤來宣告類型參數。這讓你可以製作泛型的函數、類別或類型:
範例:
/**
* Description of the class MyClass.
*
* @template T Description of the type parameter T.
*/
class MyClass<T> {
constructor(public readonly x: T) {}
}
當我在建構函數中將滑鼠懸停在 T 的出現位置時,Typescript Playground 中會顯示文字 “Description of the type parameter T.”,所以這確實似乎有效。
遊樂場連結
如果你想使用 TSDoc 而不是 JSDoc,那麼標籤是 @typeParam。
答案#2
對於正在尋找 TsDoc 的人,你可以使用 https://tsdoc.org/pages/tags/typeparam/
