`interface` 中的 Typescript 函数重载
Typescript function overloading in `interface`
我第一次在 Typescript 中使用重载,运行 在 interface
.
中定义它们时遇到了一些错误
我想我正确地理解了这个概念,因为这些示例编译没有错误:
function test(): void;
function test(str: string): string;
// OK
function test(str?: string) {
if (typeof str === 'string') return str;
};
test(); // OK
test(''); // OK
class Test2 {
public test(): void;
public test(str: string): string;
// OK
public test(str?: string) {
if (typeof str === 'string') return str;
}
constructor() {
this.test(); // OK
this.test(''); // OK
}
}
但是,这些都抛出:
Type '() => void' is not assignable to type '{ (): void; (str: string): string; }'.
Type '(str: string) => string' is not assignable to type '{ (): void; (str: string): string; }'.
interface Test {
test(): void;
test(str: string): string;
}
const test1: Test = {
test: () => {}, // ERROR
};
const test2: Test = {
test: (str: string) => str, // ERROR
};
class Test implements Test {
constructor() {
this.test = () => {}; // ERROR
this.test = (str: string) => ''; // ERROR
}
}
我是不是做错了什么,或者这是 Typescript 中的错误?
编辑:这是 Typescript Playground
中的代码
您的失败案例之所以失败,是因为在任何情况下,您分配的实现仅满足接口中定义的重载函数签名之一。
这很难做到,因为您不能在任何给定对象上定义两个同名的属性。因此,就像您的 function
声明的情况一样,您需要提供一个满足两个函数签名的实现:
interface Test {
test(): void;
test(str: string): string;
}
const test: Test = {
test: (str?: string): any => {
if (typeof str === 'string') return str;
}
};
class Test implements Test {
constructor() {
this.test = (str?: string): any => {
if (typeof str === 'string') return str;
}
}
}
如 所述,指定匹配 void
和 string
的 return 类型是非常重要的(不可能?),因此需要使用 any
.
我第一次在 Typescript 中使用重载,运行 在 interface
.
我想我正确地理解了这个概念,因为这些示例编译没有错误:
function test(): void;
function test(str: string): string;
// OK
function test(str?: string) {
if (typeof str === 'string') return str;
};
test(); // OK
test(''); // OK
class Test2 {
public test(): void;
public test(str: string): string;
// OK
public test(str?: string) {
if (typeof str === 'string') return str;
}
constructor() {
this.test(); // OK
this.test(''); // OK
}
}
但是,这些都抛出:
Type '() => void' is not assignable to type '{ (): void; (str: string): string; }'.
Type '(str: string) => string' is not assignable to type '{ (): void; (str: string): string; }'.
interface Test {
test(): void;
test(str: string): string;
}
const test1: Test = {
test: () => {}, // ERROR
};
const test2: Test = {
test: (str: string) => str, // ERROR
};
class Test implements Test {
constructor() {
this.test = () => {}; // ERROR
this.test = (str: string) => ''; // ERROR
}
}
我是不是做错了什么,或者这是 Typescript 中的错误?
编辑:这是 Typescript Playground
中的代码您的失败案例之所以失败,是因为在任何情况下,您分配的实现仅满足接口中定义的重载函数签名之一。
这很难做到,因为您不能在任何给定对象上定义两个同名的属性。因此,就像您的 function
声明的情况一样,您需要提供一个满足两个函数签名的实现:
interface Test {
test(): void;
test(str: string): string;
}
const test: Test = {
test: (str?: string): any => {
if (typeof str === 'string') return str;
}
};
class Test implements Test {
constructor() {
this.test = (str?: string): any => {
if (typeof str === 'string') return str;
}
}
}
如 void
和 string
的 return 类型是非常重要的(不可能?),因此需要使用 any
.