简体   繁体   中英

Typescript map over list of functions

Coming from a Haskell background, I would like to map over a list of functions. For example I have:

functions fnA(x){ return true }
function  fnB(x){ return false }

// I would like to write this code that applies the same value to all functions
[ fnA, fnB ] .map ( (fn : any) => fn('hello world') )

However I am getting error:

(x : any) => boolean' cannot be used as an index type.

Prefix the last line with a semicolon:

;[ fnA, fnB ] .map ( (fn : any) => fn('hello world') )

Background: semicolons are optional in Javascript. However there are some edge-cases which require you to apply some style rules.

Starting a line with a bracket [ or parenthesis ( is one of those edge cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM