1

Given a simple enum:

export enum IconNames {
  DEFAULT = 'DEFAULT',
  MOVE = 'MOVE',
  RESIZE = 'RESIZE',
  ADD = 'ADD',
  CANCEL = 'CANCEL',
  CLOSE = 'CLOSE',
}

I would like to type the name argument in the given function isTransform so I would call it only with IconNames value:

/* Tried `string` which doesn't work 
as `name` supposed to be enum's value */

const isTransform = (name: any) => [IconNames.MOVE, IconNames.RESIZE, IconNames.ADD].includes(name);

Do I have to build an interface for it? How do I use it?

Dennis Vash
  • 50,196
  • 9
  • 100
  • 118

1 Answers1

2

This seems to work:

function isTransform(name: string): boolean {
  const transformValues: string[] = [IconNames.MOVE, IconNames.RESIZE, IconNames.ADD]
  return transformValues.indexOf(name) > -1
}

This works as well:

function isTransform(name: IconNames): boolean {
  const transformValues = [IconNames.MOVE, IconNames.RESIZE, IconNames.ADD]
  return transformValues.indexOf(name) > -1
}

The question here is would you be calling isTransform only with IconNames values or with a string?

thedude
  • 9,388
  • 1
  • 29
  • 30