This is a simplified version of my code.
I want to define the type HandlerFunctionType
.
let handlerFunction: HandlerFunctionType;
// handlerFunction will be assigned with one of the following
// case 1
handlerFunction = (value: boolean) => {
// do something
return true; // or false
};
// case 2
handlerFunction = (value: number) => {
// do something
return true; // or false
};
// case 3
handlerFunction = (value: string) => {
// do something
return true; // or false
};
我可以这样界定:
export type HandlerFunctionType =
| ((value: boolean) => boolean)
| ((value: number) => boolean)
| ((value: string) => boolean);
既然这样使法典更长,而且所有这些职能都能够归还一个ool子,我想通过这样做简化:
export type HandlerFunctionType = (value: boolean | number | string) => boolean;
But, this raises error:
Type (value: boolean) => true is not assignable to type HandlerFunctionType . Types of parameters value and value are incompatible. Type string | number | boolean is not assignable to type boolean . Type string is not assignable to type boolean .
我如何简化和分类这些类型?