从构造函数调用的方法中为"只读"属性赋值



>我有一个简单的类,我想在构造函数启动的方法中为只读属性赋值,但它说[ts] Cannot assign to 'readOnlyProperty' because it is a constant or a read-only property.为什么即使我从构造函数调用process也无法为属性赋值?

示例代码:

class C {
readonly readOnlyProperty: string;
constructor(raw: string) {
this.process(raw);
}
process(raw: string) {
this.readOnlyProperty = raw; // [ts] Cannot assign to 'readOnlyProperty' because it is a constant or a read-only property.
}
}

我通常使用以下解决方法:

get myValue(): boolean { return this._myValue }
private _myValue = true

结果:

  • myValue– 从外部readonly
  • _myValue– 可以在类中修改

此解决方法的优点是IDE 可以重构属性。此外,我们不会滥用不可写的readonly属性,这可能会导致编译、优化或代码审查时出错。如果不是今天,那么在不久的将来。这就是为什么我不会使用这样的东西:

// hack, may cause problems (refactoring is not possible)
(this as any).readOnlyProperty = raw
// hack, may cause problems (we write a non-writable)
(this.readOnlyProperty as boolean) = raw
// hack, may cause problems (refactoring is not possible)
Object.assign(this, {readOnlyProperty: raw})

与其强制转换this as any,不如通过修改此属性来强制执行类型检查:

// OK
(this.readOnlyProperty as string) = raw; 
// TS2322: Type '5' is not assignable to type 'string'.
(this.readOnlyProperty as string) = 5;   

当您创建一个单独的函数来分配值时,可以从唯一构造函数以外的其他位置使用此单独的函数。编译器不会检查(对于公共函数,甚至无法检查(该函数是否仅从构造函数调用。所以错误。

无论如何,您有 2 种解决方法来分配值。清理器是将单独函数的核心放入构造函数中。另一个,它会让你失去类型检查,因此不会推荐,除非你真的知道你在做什么,否则this转换为any

(this as any).readOnlyProperty = raw

一个很老的问题,但认为仍然值得分享我通常如何解决这个问题。 我倾向于从方法中返回您要设置的值,然后您仍然在分离逻辑,同时还保持只读,没有任何可以说是非法的绕过。 例如...

class C {
readonly readOnlyProperty: string;
constructor(raw: string) {
this.readOnlyProperty = this.process(raw);
}
process(raw: string) {
// ...some logic that processes 'raw'...
return raw;
}
}

试试

Object.assign(this, {readOnlyProperty: raw})

最新更新