Home > Back-end >  Property 'MathFun' is missing in type '(x?: number, y?: number) => number' bu
Property 'MathFun' is missing in type '(x?: number, y?: number) => number' bu

Time:04-07

Not able to uderstand the error here:

Property 'MathFun' is missing in type '(x?: number, y?: number) => number' but required in type 'Func'.(2741) input.tsx(3, 5): 'MathFun' is declared here.

interface Func {
    MathFun:(a:number,b:number) => number
};


const plus:Func = (x:number=30,y:number=40):number => x y;

any one help me to understand? what is the correct way to declare interface for a function?

Live URL

CodePudding user response:

You can create a function interface by leaving out the function name and replacing the => with a : (current docs, and a slightly clearer explanation in the old docs):

interface Func {
  (a: number, b: number): number;
}

const plus: Func = (x: number = 30, y: number = 40): number => x   y;

The interface you created is valid code, but it is for an object with a function property:

interface FuncObject {
  mathFun: (a: number, b: number) => number;
}

const plusObj: FuncObject = {
  mathFun: (x: number = 30, y: number = 40): number => x   y,
};

TypeScript playground

  • Related