小编hos*_*khi的帖子

为什么接口中的这​​个箭头函数无法编译?

箭头函数常规函数在实现接口方面有什么区别,使得代码A导致编译时错误而代码B编译成功。

注意:在tsconfig.json所有严格类型检查选项均已启用的情况下,包括strictFunctionTypes,顺便说一句,它认为通过启用strict所有严格类型检查选项即可启用。

导致编译时错误的代码A

interface SomeInterface {
    someFunction: (par1: string | undefined) => string;
}

class SomeClass implements SomeInterface {
    someFunction(par1: string): string    //invalid function signature
    {
        throw new Error('Method not implemented.');
    }
}
Run Code Online (Sandbox Code Playgroud)

并且,代码B编译成功。

interface SomeInterface {
    someFunction(par1: string | undefined): string;
}

class SomeClass implements SomeInterface {
    someFunction(par1: string): string    //invalid function signature
    {
        throw new Error("Method not implemented.");
    }
}
Run Code Online (Sandbox Code Playgroud)

游乐场链接

oop typescript typescript-typings

9
推荐指数
1
解决办法
841
查看次数

标签 统计

oop ×1

typescript ×1

typescript-typings ×1