vitaly-t
vitaly-t

Reputation: 25840

Using enum as a type/interface

While converting an enumerator from JavaScript into TypeScript, I had to add my own enum - myEnumType.

If I use such enum as a property type:

prop:myEnumType

it will be expected that the value must be of type myEnumType.

How can we declare a property in TypeScript that represents the enum itself as a type, as opposed to a value of that type?

I'm trying to expose the enum as a type, via an interface property.

Upvotes: 4

Views: 15244

Answers (2)

Jayani Sumudini
Jayani Sumudini

Reputation: 1509

In myEnumType.ts class

export enum MyEnumType {
  Member1,
  Member2
}

In Foo.ts class

import { MyEnumType } from './myEnumType';
export interface Foo {
  prop: MyEnumType;
}

Upvotes: 2

basarat
basarat

Reputation: 275849

I'm trying to expose the enum as a type, via an interface property.

You can declare an enum e.g. in a vendor.d.ts:

declare enum MyEnumType {
   Member1,
   Member2,
}

Update

And how would I then declare a property of the enum as a type, via an interface property?

interface Foo {
  prop: MyEnumType
}

Upvotes: 4

Related Questions