键入具有多态类型

时间:2017-10-10 15:55:59

标签: flowtype

我正在尝试键入一个函数,该函数接受键和函数的映射,并将它们应用于对象,如果它包含映射中存在的键,则将该键上的函数应用于对象属性上的值。现在我挂断了我的转换函数地图:

export type Normalizer<A, B> = (value: A) => B;
const stringToBool: Normalizer<string, boolean> = value => value === 'true';
const stringToInt: Normalizer<string, number> = value => Number.parseInt(value);
const stringToFloat: Normalizer<string, number> = value => Number.parseFloat(value);

export type NormalizerMap = {
    [string]: Normalizer<*, *>
};

export const formNormalizer: NormalizerMap = {
    paid: stringToBool,
    minLength: stringToInt,
    minShiftLength: stringToFloat,
};

我收到以下错误:

11:     paid: stringToBool,
              ^ type application of polymorphic type: type `Normalizer`. Has some incompatible type argument with
7:     [string]: Normalizer<*, *>
                 ^ type application of polymorphic type: type `Normalizer`

    Type argument `B` is incompatible:
        2: const stringToBool: Normalizer<string, boolean> = value => value === 'true';
                                                  ^ boolean. This type is incompatible with
        3: const stringToInt: Normalizer<string, number> = value => Number.parseInt(value);
                                                 ^ number

是否可以使用多态类型执行此类操作?

0 个答案:

没有答案