quippe
quippe

Reputation: 383

typescript how to omit enum values

I got enum

enum Status {
  Cancelled = 'cancelled',
  Completed = 'completed',
  Created = 'created'
}

and I want to create another enum only with Completed and Created value

enum StatusMinimal {
  Completed = 'completed',
  Created = 'created'
}

I tried to use Omit but it works only with types. Is it even possible to do it in typescript?

Upvotes: 1

Views: 3892

Answers (3)

Greibur
Greibur

Reputation: 1

This is similar to this question. You can basically use the builtin TypeScript utility type Exclude.

Or you could do it like this :

enum StatusMinimal {
  Completed = 'completed',
  Created = 'created'
}

enum StatusExtended {
  Completed = 'completed',
  Created = 'created'
}

type Status = StatusMinimal | StatusExtended;

Upvotes: -3

Teneff
Teneff

Reputation: 32148

You can define a property/parameter based on the Status and Exclude the unwanted values

function test(s: Exclude<Status, Status.Cancelled>): void {}


test(Status.Completed)
test(Status.Created)
test(Status.Cancelled) // not assignable

playground

Upvotes: 6

Simon Hansen
Simon Hansen

Reputation: 700

As far as I know there is no trivial way. See this similiar question.

I would start with Minimal and extend the Status enum from the minimal implementation. (I know, not what your asked for)

Upvotes: 1

Related Questions