#Trying to properly type params of functions contained within a Map

4 messages · Page 1 of 1 (latest)

autumn cloud
#

Hi all. Currently trying to setup and type a zustand store. My store has actions contained within a different Map to differentiate them on their main responsibility, I'll just show one of the Map instances:

  export type ActionsObject = {
  projectActions: Map<string, (...args: unknown[]) => void>
  linesActions: Map<string, (...args: unknown[]) => void>
  holesActions: Map<string, (...args: unknown[]) => void>
  itemsActions: Map<string, (...args: unknown[]) => void>
  areaActions: Map<string, (...args: unknown[]) => void>
  viewer2DActions: Map<string, (...args: unknown[]) => void>
  viewer3DActions: Map<string, (...args: unknown[]) => void>
  verticesActions: Map<string, (...args: unknown[]) => void>
}
 ... just showing one key
  verticesActions: new Map([
      ['beginDraggingVertex', (vertexID) => beginDraggingVertex(set, get, vertexID as string)],
      ['updateDraggingVertex', (x, y) => updateDraggingVertex(set, get, x as number, y as number)],
      ['endDraggingVertex', (x, y) => endDraggingVertex(set, get, x as number, y as number)],
    ]),
...

These Maps will grow and can have any type of variables passed within. So far, I typed the arguments as unknown to later cast them, but I'm sure there is a better way to do this with generics.

worn fog
#

@autumn cloud what you are saying is that (vertexID: string) => ... doesn't work

autumn cloud
#

It does not if I use 'unknown[]' as the type of the parameters

#

I do get this if I do so: