dagda1
dagda1

Reputation: 28770

specify return type for inline functions with flow

I have the following functions:

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

How do I say that stripSpaces and safeTrim return strings.

Upvotes: 1

Views: 289

Answers (1)

luislhl
luislhl

Reputation: 1506

Your safeNull function is typed to return a function that returns a string. So all you have to do is remove those Function types from stripSpaces and safeTrim. Flow will infer that they return strings, because of safeNull return type.

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

You could also explicitly define their types, if you prefer, like so:

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

Upvotes: 1

Related Questions