箭头函数和常规函数在实现接口方面有什么区别,使得代码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)