代码之家  ›  专栏  ›  技术社区  ›  dagda1

为具有流的内联函数指定返回类型

  •  0
  • dagda1  · 技术社区  · 8 年前

    我有以下功能:

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

    1 回复  |  直到 8 年前
        1
  •  0
  •   luislhl    8 年前

    你的 safeNull 函数的类型是返回返回字符串的函数。 Function 类型来自 stripSpaces safeTrim 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());