und*_*ned 2 javascript typescript
我有一个根据选项键值返回不同类型的方法。
class Test {
getData(options: { obj: true }): Object;
getData(options: { obj: false }): any[];
getData(): any[];
getData(options = { obj: false }): Object | any[] {
if (options.obj) {
return {};
} else {
return [];
}
}
}
Run Code Online (Sandbox Code Playgroud)
当传递objas 时true,我返回对象否则数组。这很好用。
const instance = new Test();
const result = instance.getData({ obj: true }); // inffered as array
const result2 = instance.getData(); // inffered as object
Run Code Online (Sandbox Code Playgroud)
问题是当我需要使用动态值时,它会引发错误:
类型 boolean 不可分配给类型 false
function getResult(obj: boolean = false ) {
return instance.getData({ obj });
}
Run Code Online (Sandbox Code Playgroud)
问题是什么?
由于类型{ obj }仅{ obj: boolean }在编译时已知,编译器不知道选择任何重载,您必须显式提供一个重载{ obj: boolean }(因为实现签名不计为函数的公共签名) ,在这种情况下编译器不会做任何魔术:
class Test {
getData(options: { obj: true }): Object;
getData(options: { obj: false }): any[];
getData(options: { obj: boolean }): Object | any[];
getData(): any[];
// This signature is the implementation and is not conidered when resolving the method
getData(options = { obj: false }): Object | any[] {
if (options.obj) {
return {};
} else {
return [];
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
您还可以在方法签名中使用条件类型,这样可以减少签名数量:
class Test {
getData<T extends boolean>(options: { obj: T }): T extends true ? Object : any[];
getData(): any[];
// This signature is the implementation and is not conidered when resolving the method
getData(options = { obj: false }): Object | any[] {
if (options.obj) {
return {};
} else {
return [];
}
}
}
const instance = new Test();
const result = instance.getData({ obj: true }); // inffered as array
const result2 = instance.getData(); // inffered as object
function getResult(obj: boolean = false) {
return instance.getData({ obj }); // inferred as Object|any[]
}
Run Code Online (Sandbox Code Playgroud)
由于type boolean = true | false和条件类型分布在联合上,因此
T extends true ? Object : any[];将是Object|any[]when Tis boolean。当T是true时,回报将是Object,当T是false时,回报将是any所有预期
| 归档时间: |
|
| 查看次数: |
6638 次 |
| 最近记录: |