我有一个枚举:
export enum PizzaSize {
SMALL = 0,
MEDIUM = 1,
LARGE = 2
}
但在这里我想使用一些值:例如小我想说它有2个值(0,100)。我怎么能这样做?
我努力使用
export enum PizzaSize {
SMALL = {key:key, value: value},
...
}
但是打字稿不接受这个。
答案 0 :(得分:31)
仅适用于TypeScript supports numeric or string-based enums,因此您必须使用类模拟对象枚举(这将使您可以将其用作函数声明中的类型):
+
然后,您可以使用预定义的实例来访问它们的export class PizzaSize {
static readonly SMALL = new PizzaSize('SMALL', 'A small pizza');
static readonly MEDIUM = new PizzaSize('MEDIUM', 'A medium pizza');
static readonly LARGE = new PizzaSize('LARGE', 'A large pizza');
// private to disallow creating other instances of this type
private constructor(private key: string, public readonly value: any) {
}
toString() {
return this.key;
}
}
:
value
或您可能想在const mediumVal = PizzaSize.MEDIUM.value;
中定义的其他任何属性/财产类型。
由于PizzaSize
的覆盖,您还可以从对象隐式打印枚举名称/键:
toString()
答案 1 :(得分:10)
如果您需要使用Type,请尝试添加一些代码。
用法:getPizzSizeSpec(PizzaSize.small).value
enum PizzaSize {
small,
medium,
large
}
interface PizzaSizeSpec {
key: number,
value: number
}
function getPizzaSizeSpec(pizzaSize: PizzaSize): PizzaSizeSpec {
switch (pizzaSize) {
case PizzaSize.small:
return {key:0, value: 25};
case PizzaSize.medium:
return {key:0, value: 35};
case PizzaSize.large:
return {key:0, value: 50};
}
}
答案 2 :(得分:4)
尝试使用:
const pizzaSize = {
small: { key: 0, value: 25 },
medium: { key: 1, value: 35 },
large: { key: 2, value: 50 }
}
答案 3 :(得分:4)
我认为要达到您想要的效果,这样的方法会起作用
interface PizzaInfo {
name: string;
cost_multiplier: number;
}
enum PizzaSize {
SMALL,
MEDIUM,
LARGE,
}
const pizzas: Record<PizzaSize, PizzaInfo> = {
[PizzaSize.SMALL]: { name: "Small", cost_multiplier: 0.7 },
[PizzaSize.MEDIUM]: { name: "Medium", cost_multiplier: 1.0 },
[PizzaSize.LARGE]: { name: "Large", cost_multiplier: 1.5 },
};
const order = PizzaSize.SMALL;
console.log(pizzas[order].name); // "Small"
答案 4 :(得分:2)
从Typescript 3.4开始,您可以结合使用keyof typeof
和const
assertions来创建与枚举具有相同类型安全性并且仍保留复杂值的对象。
通过创建与type
同名的const
,您可以进行与普通枚举相同的穷举检查。
唯一的缺点是您需要在复杂对象中使用一些键(我在这里使用value
)来保存枚举成员的名称(如果有人可以找出可以构建这些对象的辅助函数的话)以一种类型安全的方式,我很乐意看到它!我无法工作)。
export const PizzaSize = {
small: { value: 'small', key: 0, size: 25 },
medium: { value: 'medium', key: 1, size: 35 },
large: { value: 'large', key: 2, size: 50 },
} as const
export type PizzaSize = keyof typeof PizzaSize
// if you remove any of these cases, the function won't compile
// because it can't guarantee that you've returned a string
export function order(p: PizzaSize): string {
switch (p) {
case PizzaSize.small.value: return 'just for show'
case PizzaSize.medium.value: return 'just for show'
case PizzaSize.large.value: return 'just for show'
}
}
// you can also just hardcode the strings,
// they'll be type checked
export function order(p: PizzaSize): string {
switch (p) {
case 'small': return 'just for show'
case 'medium': return 'just for show'
case 'large': return 'just for show'
}
}
在其他文件中,可以简单地使用它,只需导入PizzaSize
。
import { PizzaSize } from './pizza'
console.log(PizzaSize.small.key)
type Order = { size: PizzaSize, person: string }
还要注意,即使是通常可变的对象也无法使用as const
语法进行突变。
const Thing = {
ONE: { one: [1, 2, 3] }
} as const
// this won't compile!! Yay!!
Thing.ONE.one.splice(1, 0, 0)
答案 5 :(得分:0)
Object.freeze将其设为只读,并阻止添加更多属性:
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
final SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private int invocationCount = 0;
// Only one layout pass for M and up. Otherwise, we'll see two and the
// scale set in the first pass is reset during the second pass, so the scale we
// set doesn't stick until the 2nd pass.
@Override
public void onGlobalLayout() {
setScale(Math.min(ss.scale, getMaximumScale()), getWidth() * ss.pivotX,
getHeight() * ss.pivotY, false);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M || ++invocationCount > 1) {
getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
}
答案 6 :(得分:0)
您可以使用类型化的const实现此目的:
export const PizzaSize: {
[key: string]: { key: string, value: string };
} = {
SMALL: { key: 'key', value: 'value' }
};
(可选)您可以提取类型信息以分离接口声明:
interface PizzaSizeEnumInstance {
key: string;
value: string;
}
interface PizzaSizeEnum {
[key: string]: PizzaSizeEnumInstance;
}
export const PizzaSize: PizzaSizeEnum = {
SMALL: { key: 'key', value: 'value' }
};