属性 'MathFun' 在类型 '(x?: number, y?: number) => number' 中缺失,但在类型 'Func' 中是必需的

Property 'MathFun' is missing in type '(x?: number, y?: number) => number' but required in type 'Func'

无法理解这里的错误:

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;

谁能帮我理解一下?为函数声明 interface 的正确方法是什么?

Live URL

您可以通过省略函数名称并将 => 替换为 : (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;

您创建的接口是有效代码,但它是针对具有函数的对象 属性:

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

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

TypeScript playground