使用flow指定内联函数的返回类型

时间:2017-08-26 10:15:25

标签: flowtype

我有以下功能:

const safeNull = fn => (txt: string): string => (isNil(txt) ? '' : fn(txt));

export const stripSpaces: Function = safeNull(txt => txt.replace(/\s/g, ''));

export const safeTrim: Function = safeNull(txt => txt.trim());

如何说stripSpacessafeTrim返回字符串。

1 个答案:

答案 0 :(得分:0)

键入您的safeNull函数以返回返回字符串的函数。 因此,您只需从FunctionstripSpaces中删除safeTrim种类型。 由于safeNull返回类型,Flow会推断它们返回字符串。

const safeNull = fn => (txt: string): string => (isNil(txt) ? '' : fn(txt));

export const stripSpaces = safeNull(txt => txt.replace(/\s/g, ''));

export const safeTrim = safeNull(txt => txt.trim());

如果您愿意,也可以明确定义他们的类型,如下所示:

const safeNull = fn => (txt: string): string => (isNil(txt) ? '' : fn(txt));

export const stripSpaces: string => string = safeNull(txt => txt.replace(/\s/g, ''));

export const safeTrim: string => string = safeNull(txt => txt.trim());