[英]In Typescript, how can I specify the return type of a function that can return multiple types?
I have a reusable method getFilters
that can return a number of different types我有一个可重用的方法
getFilters
可以返回许多不同类型
getFilters(): IListFilteringType {...}
type IListFilteringTypeMultiSelect = (string | number)[];
type IListFilteringType = boolean | string | number | IListFilteringTypeMultiSelect;
When I call getFilters
how can I specify that I know that the returned value with be of type IListFilteringTypeMultiSelect
and not one of the other possible values of IListFilteringType
?当我打电话
getFilters
我怎么可以指定我知道,与返回的值是类型的IListFilteringTypeMultiSelect
,而不是其他的可能值之一IListFilteringType
?
Thank you谢谢
如果您确定特定执行的函数返回的类型,则可以使用as
返回值。
const variable = functionResponse as IListFilteringTypeMultiSelect;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.