Typescriptでは、次のような列挙型があります
export enum CarBrands {
Toyota = "TOYOTA"
Ford = "FORD"
.....
}
この列挙型のサブセットをこのように作成したいのですが、できないようです
enum JapaneseCars {
CarBrands.Toyota
}
またはサブセットでオブジェクトを作成する
const JapaneseCars = {
Carbrands.Toyota
}
別の既存の列挙型の値を使用するオブジェクトまたは列挙型を作成する方法はありますか?
CarBrands列挙型を他のデータ型に変更することはできません
ベストアンサー1
別の列挙型のサブセットである列挙型のように動作するtype
and の組み合わせを作成できます。const
enum
欠点: このソリューションでは、任意のキーがその値と一致するようにする必要があります。(または、そのケースを処理する解決策をまだ見つけていません)
// 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.
ただし、 型の不明な値をCarBrand
型の値にマップする場合FrenchCarBrand
は機能しません。
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.
そのためには型ガードが必要です:
export const isFrenchCarBrand = (brand: CarBrand): brand is FrenchCarBrand => {
return brand in FrenchCarBrand;
}
望ましくない値に遭遇した場合にどうするかを決めることができます
if (isFrenchCarBrand(brand)) {
// You can safely assume that `brand` is a `FrenchCarBrand`
} else {
// `brand` is definitely not a `FrenchCarBrand`
}