admin管理员组文章数量:1315338
I have a method that returns different type based on option key value.
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 [];
}
}
}
When passing the obj
as true
, I'm returning object otherwise array. That works fine.
const instance = new Test();
const result = instance.getData({ obj: true }); // inffered as array
const result2 = instance.getData(); // inffered as object
The problem is when I need to use dynamic value it throws an error:
type boolean is not assignable to type false
function getResult(obj: boolean = false ) {
return instance.getData({ obj });
}
What is the problem?
I have a method that returns different type based on option key value.
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 [];
}
}
}
When passing the obj
as true
, I'm returning object otherwise array. That works fine.
const instance = new Test();
const result = instance.getData({ obj: true }); // inffered as array
const result2 = instance.getData(); // inffered as object
The problem is when I need to use dynamic value it throws an error:
type boolean is not assignable to type false
function getResult(obj: boolean = false ) {
return instance.getData({ obj });
}
What is the problem?
Share Improve this question asked Jun 19, 2018 at 16:02 undefinedundefined 6,88413 gold badges53 silver badges101 bronze badges 02 Answers
Reset to default 7Since the type of { obj }
is only known as { obj: boolean }
at pile time, the piler can't know to pick any of the overloads, you have to explicitly supply an overload that takes { obj: boolean }
(since the implementation signature does not count as a public signature for the function), the piler will not do any magic in this case:
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 [];
}
}
}
Edit
You can also use conditional types in the method signature and this will keep the number of signatures lower:
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[]
}
Since type boolean = true | false
and conditional types distribute over unions,
T extends true ? Object : any[];
will be Object|any[]
when T
is boolean
. When T
is true
, the return will be Object
and when T
is false
, the return will be any
all as expected
You cannot overload methods in TypeScript like you do in C#, for example. You need to bine types, like this:
class Test {
getData(options: {obj: boolean} = { obj: false }): Object | any[] {
if (options.obj) {
return {};
} else {
return [];
}
}
}
When you redeclare the function with the same name several times, you will just get the last one as the final definition, in runtime.
本文标签: javascriptTypescript overloading type boolean is not assignable to type falseStack Overflow
版权声明:本文标题:javascript - Typescript overloading type boolean is not assignable to type false - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741977501a2408209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论