0
votes

Only action === 'enable' in if is causing 'Cannot invoke an expression whose type lacks a call signature. Type '((value?: any, options?: Object) => void) | ((opts?: { onlySelf?: boolean; emitEvent?: boolean; }) => void)' has no compatible call signatures.' error in the line of formGroup.get, if I remove this snippet on the if the error does not happen.

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 <<<<<<<<<<<<
   }
}

At other times I call the function.

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

formGroup initiation

const formGroup = this.formBuilder.group({
   field1: [{ value: undefined, disabled: false }, Validators.compose([])],
   field2: [
      { value: 'text' },
      Validators.compose([Validators.required])
      ],
....
return formGroup
1
show formGroup initiation – Sachila Ranawaka
edited: formGroup initiation – Eduardo Dallmann

1 Answers

1
votes

I'm guessing you are using TypeScript 3.2 or below. If you can update to TypeScript 3.3 or above, there is more support for calling unions of functions and your code should hopefully not error at all:

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

πŸ”—πŸ‘©β€πŸ’»

If you are stuck with an older version of TypeScript, the best way to deal with this is to use a type assertion to tell the compiler that you are sure what you are doing is safe:

// 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

πŸ”—πŸ‘©β€πŸ’»

Hope that helps. Good luck!