Rush Stack商店博客活动
跳至主要内容

@readonly

标签类型:修饰符

TSDoc 标准化: 扩展

此修饰符标签与类或接口的属性一起使用。它表示该属性应被记录为只读,即使类型签名指示了其他情况。

此修饰符仅在特殊情况下需要。通常,API 提取器会使用属性的类型签名来确定它是否为只读。

但是,假设类属性有一个始终抛出异常的 setter 函数,解释了该属性无法被赋值。在这种情况下,应添加 @readonly 修饰符,以便该属性被记录为只读。

用法示例

/** @public */
export class Widget {
...
/**
* Everyday case: API Extractor will document this property as being read-only.
*/
public get x(): number {
return this._x;
}

/**
* Special case: We need to tell API Extractor to ignore the property setter.
* @readonly
*/
public get title(): string {
return this._title;
}

public set title(value: string) {
throw new Error('This property is read-only!');
}
}