简体   繁体   English

如何在 TypeScript 中处理此方法链接? 错误 ts(2339)

[英]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.

相关问题 ts2339错误:如何在vuejs项目中使用typescript mixin? - ts2339 error: how to use typescript mixin in vuejs project? 错误TS2339:JavaScript到Typescript错误 - error TS2339: JavaScript to Typescript error Typescript/ESLint:覆盖 getter 并调用 super 方法(错误 ts(2340) 和 ts(2339)) - Typescript/ESLint : override getter and call the super method (error ts(2340) and ts(2339)) 打字稿错误-ComponentRef上的TS2339 - Typescript Error - TS2339 on ComponentRef 如何删除打字稿中的错误错误(错误:TS2339)? - how do you remove false error in typescript (error: TS2339)? Typescript:如何以字符串形式获取函数名称而不会出现TS2339错误 - Typescript: how to get function name as string without TS2339 error 如何解决打字稿错误 TS2339:“IntrinsicAttributes & …”类型上不存在“XXX”属性? - How to resolve typescript error TS2339: Property 'XXX' does not exist on type 'IntrinsicAttributes & …? 如何忽略 TypeScript 错误 TS2339 - 类型上不存在属性 - How to ignore TypeScript error TS2339 - Property does not exist on type 如何解决TypeScript编译器错误TS2339:我的Angular 5应用程序中类型{}上不存在属性'errorValue' - How to resolve TypeScript compiler error TS2339: Property 'errorValue' does not exist on type {} in my Angular 5 application 如何停止 typescript 错误“'HTMLElement'.ts(2339) 类型上不存在属性'play'” - how to stop the typescript error `Property 'play' does not exist on type 'HTMLElement'.ts(2339)`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM