假设我有两个枚举,如下面的Typescript中所述,那么我如何合并它们
enum Mammals {
Humans,
Bats,
Dolphins
}
enum Reptiles {
Snakes,
Alligators,
Lizards
}
export default Mammals & Reptiles // For Illustration purpose, Consider both the Enums have been merged.
现在,当我在另一个文件中import
exported value
时,我应该能够从两个枚举中访问值。
import animalTypes from "./animalTypes"
animalTypes.Humans //valid
animalTypes.Snakes // valid
如何在Typescript中实现此类功能?
我不会提出合并到枚举的解决方案(我找不到合适的方法)
但是如果你想要从你使用它的方式表现得像枚举,你仍然可以在javascript中使用合并对象。
enum Mammals {
Humans = 'Humans',
Bats = 'Bats',
Dolphins = 'Dolphins',
}
enum Reptiles {
Snakes = 'Snakes',
Alligators = 'Alligators',
Lizards = 'Lizards',
}
const Animals = {
...Mammals,
...Reptiles,
}
然后你可以使用Animals.Snakes或Animals.Dolphins,两者都应该正确输入并作为枚举
这里令人困惑的是类型和。值。
let
,const
等),它将有一个值加上一些计算但不单独命名的类型。type
或interface
,它将创建一个命名类型,但不会以任何方式在最终的JS中输出或考虑。它只在编写应用程序时有所帮助。enum
,它会创建一个静态类型名称,您可以使用该名称以及输出到JS的真实对象,您可以使用它。从TS手册:
使用枚举很简单:只需将任何成员作为枚举本身的属性进行访问,并使用枚举的名称声明类型。
所以,如果你Object.assign()
两个枚举,它将创建一个新的合并值(对象),但不是一个新的命名类型。
由于它不再是enum
,您将失去拥有值和命名类型的优势,但您仍然可以创建单独的类型名称作为变通方法。
幸运的是,您可以为值和类型使用相同的名称,如果导出它们,TS将导入它们。
// This creates a merged enum, but not a type
const Animals = Object.assign({}, Mammals, Reptiles);
// Workaround: create a named type (typeof Animals won't work here!)
type Animals = Mammals | Reptiles;
TypeScript枚举不仅包含您定义的键,还包含数字反转,例如:
Mammals.Humans === 0 && Mammals[0] === 'Humans'
现在,如果您尝试合并它们 - 例如使用Object#assign
- 您最终会得到两个具有相同数值的键:
const AnimalTypes = Object.assign({}, Mammals, Reptiles);
console.log(AnimalTypes.Humans === AnimalTypes.Snakes) // true
而且我想这不是你想要的。
防止这种情况的一种方法是手动将值分配给枚举并确保它们不同:
enum Mammals {
Humans = 0,
Bats = 1,
Dolphins = 2
}
enum Reptiles {
Snakes = 3,
Alligators = 4,
Lizards = 5
}
或者不那么明确但是等同于:
enum Mammals {
Humans,
Bats,
Dolphins
}
enum Reptiles {
Snakes = 3,
Alligators,
Lizards
}
无论如何,只要您确保合并的枚举具有不同的键/值集,您就可以将它们与Object#assign
合并。
我想说正确的方法是定义一个新类型:
enum Mammals {
Humans = 'Humans',
Bats = 'Bats',
Dolphins = 'Dolphins',
}
enum Reptiles {
Snakes = 'Snakes',
Alligators = 'Alligators',
Lizards = 'Lizards',
}
type Animals = Mammals | Reptiles;
enum AA1 {
aKey, // = 0
bKey // = 1
}
enum BB1 {
cKey, // = 0
dKey // = 1
}
enum AA2 {
aKey = 1
}
enum BB2 {
aKey = 2
}
enum AA3 {
aKey, // = 0
bKey // = 1
}
enum BB3 {
cKey = 2,
dKey // = 3
}
enum AA4 {
aKey = 'Hello',
bKey = 0,
cKey // = 1
}
enum BB4 {
dKey = 2,
eKey = 'Hello',
fKey = 'World'
}
注意:aKey = 'Hello'
和eKey = 'Hello'
工作,因为带有字符串值的枚举不具有此值作为键
// For aKey = 'Hello', key is working
type aa4aKey = AA4.aKey; // = AA4.aKey
// value is not.
type aa4aValue = AA4.Hello; // ❌ Namespace 'AA4' has no exported member 'Hello'
type aa4aValue2 = AA4['Hello']; // ❌ Property 'Hello' does not exist on type 'AA4'
console.log(AA4); // { 0: 'bKey', 1: 'cKey', aKey: 'Hello', bKey: 0, cKey: 1 }
console.log(BB4); // { 2: 'dKey', dKey: 2, eKey: 'Hello', fKey: 'World' }
type AABB1 = AA4 | BB4; // = AA4 | BB4
type AABB1key = AABB1['aKey']; // = never
type AABB1key2 = AABB1.aKey; // ❌ 'AABB1' only refers to a type, but is being used as a namespace here. ts(2702)
type AABB1 = AA4 & BB4; // = never
type AABB1key = AABB1['aKey']; // = never
type AABB2 = (typeof AA4) & (typeof BB4); // = typeof AA4 & typeof BB4
type AABB2key = AABB2['aKey']; // = AA4.aKey
const aabb1 = { ...AA4, ...BB4 };
const aabb2 = Object.assign({}, AA4, BB4); // also work
// aabb1 = {
// 0: 'bKey',
// 1: 'cKey',
// 2: 'dKey',
// aKey: 'Hello',
// bKey: 0,
// cKey: 1,
// dKey: 2,
// eKey: 'Hello',
// fKey: 'World' }
const aabb = { ...AA4, ...BB4 };
type TypeofAABB = typeof aabb;
// type TypeofAABB = {
// [x: number]: string;
// dKey: BB4.dKey;
// eKey: BB4.eKey;
// fKey: BB4.fKey;
// aKey: AA4.aKey;
// bKey: AA4.bKey;
// cKey: AA4.cKey;
// };
提示:您可以对类型和值使用相同的名称
const merged = { ...AA4, ...BB4 };
type merged = typeof merged;
const aValue = merged.aKey;
type aType = merged['aKey'];
如果你想合并你的2个枚举,你有~3个选择:
enum Mammals {
Humans = 'Humans',
Bats = 'Bats',
Dolphins = 'Dolphins'
}
enum Reptiles {
Snakes = 'Snakes',
Alligators = 'Alligators',
Lizards = 'Lizards'
}
export const Animals = { ...Mammals, ...Reptiles };
export type Animals = typeof Animals;
enum Mammals {
Humans = 0,
Bats,
Dolphins
}
enum Reptiles {
Snakes = 2,
Alligators,
Lizards
}
export const Animals = { ...Mammals, ...Reptiles };
export type Animals = typeof Animals;
enum Mammals {
Humans,
Bats,
Dolphins
}
enum Reptiles {
Snakes,
Alligators,
Lizards
}
export const Animals = { Mammals, Reptiles };
export type Animals = typeof Animals;
const bats = Animals.Mammals.Bats; // = 1
const alligators = Animals.Reptiles.Alligators; // = 1
试试这个枚举示例------
枚举或枚举是TypeScript支持的新数据类型
enum PrintMedia {
Newspaper = 1,
Newsletter,
Magazine,
Book
}
function getMedia(mediaName: string): PrintMedia {
if ( mediaName === 'Forbes' || mediaName === 'Outlook') {
return PrintMedia.Magazine;
}
}
let mediaType: PrintMedia = getMedia('Forbes');