In Typescript, I have an enum like
export enum CarBrands {
Toyota = "TOYOTA"
Ford = "FORD"
.....
}
I would like to create a subset of this enum like this but cannot seem to do it
enum JapaneseCars {
CarBrands.Toyota
}
or create an object with a subset
const JapaneseCars = {
Carbrands.Toyota
}
Is there anyway i can create an object or enum that uses that values from another existing enum?
I cannot change the CarBrands enum to be some other data type
You can create a type
and const
combination that will behave like an enum that is the subset of another enum.
Drawback: this solution forces you to have any enum
's key match their value (or I have not found a solution that handles that case yet)
// Given an enum
export enum CarBrand {
TOYOTA = 'TOYOTA',
FORD = 'FORD',
PEUGEOT = 'PEUGEOT',
RENAULT = 'RENAULT'
}
// We can create the type of the subset
export type FrenchCarBrand = Extract<CarBrand, CarBrand.PEUGEOT | CarBrand.RENAULT>;
// And the const that goes with it
export const FrenchCarBrand = {
[CarBrand.PEUGEOT]: CarBrand.PEUGEOT,
[CarBrand.RENAULT]: CarBrand.RENAULT
} as const;
// With that you can :
// assign a CarBrand to a FrenchCarBrand ...
const a: FrenchCarBrand = CarBrand.RENAULT;
// ... as long as the CarBrand is in the type
const a_invalid: FrenchCarBrand = CarBrand.TOYOTA; // Type 'CarBrand.TOYOTA' is not assignable to type 'FrenchCarBrand'.
// assign a FrenchCarBrand to a CarBrand ...
const b: CarBrand = FrenchCarBrand.PEUGEOT;
// ... as long as the key exists
const b_invalid: CarBrand = FrenchCarBrand.TOYOTA; // Property 'TOYOTA' does not exist on type 'Readonly<Record<FrenchCarBrand, FrenchCarBrand>>'
// You cannot reassign the value of a FrenchCarBrand ...
FrenchCarBrand.PEUGEOT = CarBrand.RENAULT; // Cannot assign to 'PEUGEOT' because it is a read-only property.
// ... just like you cannot reassign the value of an enum
CarBrand.TOYOTA = 'MAZDA'; // Cannot assign to 'TOYOTA' because it is a read-only property.
If you want to map an unknown value of type CarBrand
to a value of type FrenchCarBrand
, it will not work though :
declare const brand: CarBrand;
const frenchBrand: FrenchCarBrand = brand; // Type 'CarBrand' is not assignable to type 'FrenchCarBrand'
// You can use the `as` keyword but it will hide bugs, so I do not recommend at all.
const frenchBrandNoError: FrenchCarBrand = brand as FrenchCarBrand; // No error, but a possible bug in your application.
For that you need a type guard :
export const isFrenchCarBrand = (brand: CarBrand): brand is FrenchCarBrand => {
return brand in FrenchCarBrand;
}
It will allow you to decide what to do in case you encounter an unwanted value
if (isFrenchCarBrand(brand)) {
// You can safely assume that `brand` is a `FrenchCarBrand`
} else {
// `brand` is definitely not a `FrenchCarBrand`
}
I have made a TypeScript Playground that show all this code with type checking in action
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With