User-Defined Extended Enums in TypeScript

0 votes

I am writing a TypeScript library that uses enums, like this:

export enum Fruit {
  // enum values must be integers,
  // because the upstream library expects them that way.
  Apple = 0,
  Banana = 1,
}

/** A wrapper function for `upstreamLibrary.useFruit`. */
export function useFruit(fruit: Fruit) {
  upstreamLibrary.useFruit(fruit as number);
  // TODO: Do more stuff.
}

By default, the library ships with Apples and Bananas, but I want end-users to be able to create their own fruit. Right now, they can do that like the following:

import { useFruit } from "fruits";

enum FruitCustom {
  // `upstreamLibrary.getFruitValue` returns an integer.
  // It is guaranteed to never overlap with any other Fruit values.
  Pear = upstreamLibrary.getFruitValue("Pear"),
}

useFruit(FruitCustom.Pear);

However, this will throw a compiler error, because FruitCustom does not match Fruit. So, in my library, I have to type the useFruit function like this:

export function useFruit(fruit: Fruit | number) {}

Unfortunately, Fruit | number resolves to just number, so now I've lost type safety, which sucks!

What's the best way to handle this?

Additional discussion: We can't use enum merging to solve this problem because you can only do that with local enums. Because Fruit comes from a 3rd party library, TypeScript won't let you do that.

Other requirements: If the solution involves not using enum, then it is necessary to be able to iterate over the members of the enum, like you can with normal enums.

Jul 18, 2022 in TypeSript by Logan
• 2,140 points
494 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In TypeSript

0 votes
1 answer
0 votes
1 answer

How to create ES6 Map in Typescript

Refer this as an example this.configs = new ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
411 views
0 votes
1 answer

What is TypeScript and why would I use it in place of JavaScript?

TypeScript is a superset of JavaScript which primarily ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
317 views
0 votes
1 answer

How to declare and initialize a Dictionary in Typescript

Apparently this doesn't work when passing the ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
5,577 views
0 votes
1 answer

What is "not assignable to parameter of type never" error in TypeScript?

All you have to do is define ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
48,758 views
0 votes
1 answer

How do you typecast in TypeScript?

Casting: return this.createMarkerStyle(<MarkerSymbolInfo> symbolInfo); Or Casting compatible with tsx mode: return ...READ MORE

answered Jun 1, 2022 in TypeSript by Nina
• 3,060 points
389 views
0 votes
1 answer

Cannot access web3 object with typescript and ethereum

You still need to instantiate it first. ...READ MORE

answered Sep 25, 2018 in Blockchain by slayer
• 29,350 points
2,661 views
0 votes
1 answer

List all the types of user-defined functions

There are three types of user-defined functions, ...READ MORE

answered Oct 29, 2018 in Database by Sahiti
• 6,370 points
1,895 views
0 votes
1 answer

How to apply zoom animation for each element of a list in angular?

Hey @Sid, do check if this link ...READ MORE

answered Jul 30, 2019 in Others by Vardhan
• 13,190 points
1,229 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP