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?

1

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I updated the question, sorry, yes I want to call it with IconNames values
see the second example in my answer, doesn't it answer this usecase?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.