Search code examples
typescriptflowtype

How do I translate this lambda type from Flow to TypeScript?


I am trying to convert the following code snippet from Flow to TypeScript

let headAndLines = headerAndRemainingLines(lines, spaceCountToTab),
      header: string[] = headAndLines.header,
      groups: string[][]= headAndLines.groups,
      arrToObjs: (string[]) => {[string]: any}[] = makeArrayToObjectsFunction(errorInfo, spaceCountToTab, header),
      ....

As it stands TS does not like the lambda type of arrToObjs.

Unexpected token, expected ";" (361:28)
359 | header: string[] = headAndLines.header,
360 | groups: string[][]= headAndLines.groups,
361 | arrToObjs: (string[]) => {[string]: any}[] = makeArrayToObjectsFunction(errorInfo, spaceCountToTab, header),

I've tried surrounding the whole expression with brackets

(string[]) => {[string]: any}[]) 

but that doesn't help.

arrToObjs is a function that takes an array of strings and returns an array of maps of {string: any}. What is the correct way to write this type signature in TypeScript?


Solution

  • You need to name your variables:

    let arrToObjs:(x: string[]) => { [key: string]: any }[];
    

    Also, if you return an Array of Maps, you could use the Map type:

    let arrToObjs:(x: string[]) => Map<string, any>[];