数组函数表达式作为类型

时间:2019-04-12 20:15:51

标签: typescript

type Predicate = 'truthy' | 'falsy' | Function;

我想使“功能”更具体,但不幸的是我做不到

type Predicate = 'truthy' | 'falsy' | () => boolean;

我知道我总能做到

someFunction(parameter: 'truthy' | 'falsy' | () => boolean) {...}

但是我想用这三个选项定义一个类型。我能做到吗?

1 个答案:

答案 0 :(得分:2)

您几乎拥有了它。由于优先规则,必须将函数类型放在括号中:

type Predicate = 'truthy' | 'falsy' | (() => boolean);

当然,您也可以单独定义函数类型:

type BooleanFunctionPredicate = () => boolean;
type Predicate = 'truthy' | 'falsy' | BooleanFunctionPredicate;
相关问题