英文:
Type conversion: Convert type of Object with uniformly typed fields to Array type in Typescript
问题
如果我想将字段类型均为字符串的对象的类型转换为字符串数组类型,该怎么做?
// index.d.ts
const event = {
RAIN: "RAIN",
SUN: "SUN",
CLOUDY: "CLOUDY",
}
// 我只能使用这个
export type eventType = (typeof event)[keyof typeof event]
如何将eventType
转换为字符串数组类型?
EDIT:我应该补充一下,我不能控制event
类型,它来自第三方。
英文:
Say I want to convert an the type of an object whose fields are uniformly typed, as string for example:
// index.d.ts
const event = {
RAIN: "RAIN",
SUN: "SUN",
CLOUDY: "CLOUDY",
}
// I can only use this
export type eventType = (typeof event)[keyof typeof event]
How would I go about converting that eventType
to the Array of strings type ?
EDIT : I should have added that I don't control the event
type, it's from a third party
答案1
得分: 1
Sure, here are the translated code snippets:
你必须使用 [const assertion](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions) 来防止编译器将值的类型扩展为 `string`:
```typescript
const events = {
RAIN: "RAIN",
SUN: "SUN",
CLOUDY: "CLOUDY",
} as const
// type EventType = {
// readonly RAIN: "RAIN";
// readonly SUN: "SUN";
// readonly CLOUDY: "CLOUDY";
// }
type EventType = typeof events
更新
由于对象来自第三方且无法操作,我唯一想到的方法是创建另一个对象,使其满足原始对象并进行 const 断言:
const myEvents ={
RAIN: 'RAIN',
SUN: 'SUN',
CLOUDY: 'CLOUDY'
} as const satisfies typeof events;
type EventType = typeof myEvents
请注意,上述代码中的 satisfies
不是 TypeScript 中的合法关键字,可能需要根据实际情况调整代码。
英文:
You have to use const assertion to prevent the compiler from extending the type of values to string
:
const events = {
RAIN: "RAIN",
SUN: "SUN",
CLOUDY: "CLOUDY",
} as const
// type EventType = {
// readonly RAIN: "RAIN";
// readonly SUN: "SUN";
// readonly CLOUDY: "CLOUDY";
// }
type EventType = typeof events
Update
Since the object is coming from a third party I can't be manipulated, the only approach I could think of is to create another object that satisfies the original one and const assert it:
const myEvents ={
RAIN: 'RAIN',
SUN: 'SUN',
CLOUDY: 'CLOUDY'
} as const satisfies typeof events;
type EventType = typeof myEvents
答案2
得分: 1
const event1 = {
RAIN: "雨",
SUN: "太阳",
CLOUDY: "多云",
}
type EventType = typeof event1
const keys1 = Object.keys(event1) as (keyof typeof event1)[]
const event2 = {
RAIN: "雨",
SUN: "太阳",
CLOUDY: "多云",
} as const
type EventType2 = typeof event2
type ValuesOf<T> = T[keyof T];
const values2 = Object.values(event2) as ValuesOf<typeof event2>[];
enum event3 {
RAIN = "雨",
SUN = "太阳",
CLOUDY = "多云",
}
type EventType3 = event3
const values3 = Object.values(event3) as event3[]
英文:
const event1 = {
RAIN: "RAIN",
SUN: "SUN",
CLOUDY: "CLOUDY",
}
type EventType = typeof event1
// type EventType = {
// RAIN: string;
// SUN: string;
// CLOUDY: string;
// }
const keys1 = Object.keys(event1) as (keyof typeof event1)[]
// const keys1: ("RAIN" | "SUN" | "CLOUDY")[]
const event2 = {
RAIN: "RAIN",
SUN: "SUN",
CLOUDY: "CLOUDY",
} as const
type EventType2 = typeof event2
// type EventType2 = {
// readonly RAIN: "RAIN";
// readonly SUN: "SUN";
// readonly CLOUDY: "CLOUDY";
// }
type ValuesOf<T> = T[keyof T];
const values2 = Object.values(event3) as ValuesOf<typeof event2>[];
// const values2: ("RAIN" | "SUN" | "CLOUDY")[]
enum event3 {
RAIN = "RAIN",
SUN = "SUN",
CLOUDY = "CLOUDY",
}
// const event3 = {
// RAIN: "RAIN",
// SUN: "SUN",
// CLOUDY: "CLOUDY",
// }
type EventType3 = event3
// ~= type EventType3 = "RAIN" | "SUN" | "CLOUDY"
// ~= type EventType3 = event3.RAIN | event3.SUN | event3.CLOUDY
const values3 = Object.values(event3) as event3[]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论