我有以下功能:
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());
如何说stripSpaces
和safeTrim
返回字符串。
答案 0 :(得分:0)
键入您的safeNull
函数以返回返回字符串的函数。
因此,您只需从Function
和stripSpaces
中删除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());