[英]How to approach this method chaining in TypeScript? Error ts(2339)
I'm receiving a ts(2339) error in attempting to use method chaining on flexible methods that can be used to both get and set a private properties.我在尝试对可用于获取和设置私有属性的灵活方法使用方法链接时收到 ts(2339) 错误。
Suppose I have the following class:假设我有以下 class:
export default class A {
private _propertyA!: number;
private _propertyB!: string;
constructor() {
this._propertyA;
this._propertyB;
this._init();
}
private _init() {
// Do something in initializing the class.
};
public getPropertyA() {
return this._propertyA;
}
public getPropertyB() {
return this._propertyB;
}
public propertyA(value?: number): number | this {
if (value === undefined) return this.getPropertyA();
this.setPropertyA(value);
return this;
}
public propertyB(value?: string): string | this {
if (value === undefined) return this.getPropertyB();
this.setPropertyB(value);
return this;
}
public setPropertyA(value: number): this {
this._propertyA = value;
return this;
}
public setPropertyB(value: string): this {
this._propertyB = value;
return this;
}
}
I have "getters" via get
prefix on methods and "setters" via set
prefix on methods.我通过在方法上get
前缀获得“getters”,通过在方法上set
前缀获得“setters”。 Then there are "shorthand" methods that are flexible that can either get or set a private property depending on whether or not an argument is provided to the method.然后是灵活的“速记”方法,可以根据是否向方法提供参数来获取或设置私有属性。
When I attempt to use the following method chaining, it results in a TypeScript error (see code comments):当我尝试使用以下方法链接时,会导致 TypeScript 错误(请参阅代码注释):
import A from './a';
// Results in the following TypeScript error:
const a = new A()
.propertyA(100)
.propertyB('value');
// Property 'propertyB' does not exist on type 'number | A'.
// Property 'propertyB' does not exist on type 'number'.ts(2339)
// Results in the following TypeScript error:
const a = new A()
.propertyB('value')
.propertyA(100);
// Property 'propertyA' does not exist on type 'string | A'.
// Property 'propertyA' does not exist on type 'string'.ts(2339)
// This works but we are not chaining.
const a = new A()
.propertyA(100);
// Works as expected.
const a = new A()
.setPropertyA(100)
.setPropertyB('value');
TypeScript Version 4.6.3 TypeScript 版本 4.6.3
It seems to me that TypesSript is not recognizing the return type is this
when there is no argument provided to these methods and the value
parameter is undefined
.在我看来,当没有为这些方法提供任何参数并且value
参数为undefined
时,TypesSript 无法识别返回类型 is this
。 And the error is introduced in method chaining.错误是在方法链接中引入的。
Is there a bug that I'm overlooking or how can this be adjusted to make these shorthand methods work?是否有我忽略的错误,或者如何调整它以使这些速记方法起作用?
Many thanks!非常感谢!
Use overloads:使用重载:
public propertyA(): number;
public propertyA(value: number): this;
public propertyA(value?: number): number | this {
if (value === undefined) return this.getPropertyA();
this.setPropertyA(value);
return this;
}
Then when you call without parameters it returns a number, and if you provide a value it's this
:然后当你不带参数调用时它返回一个数字,如果你提供一个值它是this
的:
new A().propertyA(); // number
new A()
.propertyA(100)
.propertyB("foo") // assuming you did the same for propertyB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.