【问题标题】:Error message: Cannot invoke an expression whose type lacks a call signature错误消息:无法调用类型缺少调用签名的表达式
【发布时间】:2019-09-07 16:28:04
【问题描述】:

只有if 中的action === 'enable' 导致'无法调用类型缺少调用签名的表达式。键入'((值?:任何,选项?:对象) => 无效)| ((opts?: { onlySelf?: boolean; emitEvent?: boolean; }) => void)' 没有兼容的调用签名。' formGroup.get 行中的错误,如果我删除这个 sn-p在if 上不会发生错误。

public act(action: string) {
   this.formGroup.get('field1')[action]();
   if (action === 'reset' || (this.userPermissions.field2 === true && action === 'enable')) {
      this.formGroup.get('field2')[action](); //error here <<<<<<<<<<<<
   }
}

其他时候我调用这个函数。

this.act('reset');
this.act('enable');
this.act('disable');
//etc

formGroup 启动

const formGroup = this.formBuilder.group({
   field1: [{ value: undefined, disabled: false }, Validators.compose([])],
   field2: [
      { value: 'text' },
      Validators.compose([Validators.required])
      ],
....
return formGroup

【问题讨论】:

  • 显示表单组启动
  • 已编辑:formGroup 启动

标签: angular typescript


【解决方案1】:

我猜你正在使用 TypeScript 3.2 或更低版本。如果您可以更新到 TypeScript 3.3 或更高版本,还有更多 support for calling unions of functions 并且您的代码应该不会出错:

// TS 3.3 or above
declare const f: ((value?: any, options?: Object) => void) | 
  ((opts?: { onlySelf?: boolean; emitEvent?: boolean; }) => void);
f(); // okay

??‍?

如果你被旧版本的 TypeScript 卡住,最好的处理方法是使用 type assertion 告诉编译器你确定你正在做的事情是安全的:

// TS 3.2 or below
declare const f: ((value?: any, options?: Object) => void) | 
  ((opts?: { onlySelf?: boolean; emitEvent?: boolean; }) => void);
f(); // error
(f as ()=>void)(); // okay

??‍?

希望对您有所帮助。祝你好运!

【讨论】:

  • 你很确定
猜你喜欢
  • 2017-02-03
  • 2019-09-10
  • 2019-12-15
  • 2018-12-25
  • 2023-03-20
  • 1970-01-01
  • 2015-08-26
  • 2017-09-01
相关资源
最近更新 更多