可以在 interface
type
和 类
里使用 readonly 修饰属性
class AbstractExtension<T, U> {
public readonly type: ExtensionType;
public readonly manifest: IExtensionManifest;
public readonly extensionLifeCycle: IExtensionLifeCycle<T, U>;
constructor(
type: ExtensionType,
manifest: IExtensionManifest,
extensionLifeCycle: IExtensionLifeCycle<T, U>
) {
this.type = type;
this.manifest = manifest;
this.extensionLifeCycle = extensionLifeCycle;
}
}
指定一个类的属性为只读,然后在声明时或者构造函数中初始化它们
class Foo {
readonly bar = 1; // OK
readonly baz: string;
constructor() {
this.baz = 'hello'; // OK
}
}