【问题标题】:specify return type for inline functions with flow使用流指定内联函数的返回类型
【发布时间】:2017-08-30 07:37:47
【问题描述】:

我有以下功能:

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 返回字符串怎么说。

【问题讨论】:

    标签: flowtype


    【解决方案1】:

    您的safeNull 函数被键入以返回一个返回字符串的函数。 所以你所要做的就是从stripSpacessafeTrim 中删除那些Function 类型。 Flow 将推断它们返回字符串,因为 safeNull 返回类型。

    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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 2015-03-23
      • 2018-04-09
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      相关资源
      最近更新 更多