在Typescript中进行类型转换:将具有统一类型字段的对象转换为数组类型。

huangapple go评论67阅读模式
英文:

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 eventtype, 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

playground

更新

由于对象来自第三方且无法操作,我唯一想到的方法是创建另一个对象,使其满足原始对象并进行 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

playground

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: &quot;RAIN&quot;,
    SUN: &quot;SUN&quot;,
    CLOUDY: &quot;CLOUDY&quot;,
}
type EventType = typeof event1
// type EventType = {
//     RAIN: string;
//     SUN: string;
//     CLOUDY: string;
// }
const keys1 = Object.keys(event1) as (keyof typeof event1)[]
// const keys1: (&quot;RAIN&quot; | &quot;SUN&quot; | &quot;CLOUDY&quot;)[]

const event2 = {
    RAIN: &quot;RAIN&quot;,
    SUN: &quot;SUN&quot;,
    CLOUDY: &quot;CLOUDY&quot;,
} as const
type EventType2 = typeof event2
// type EventType2 = {
//     readonly RAIN: &quot;RAIN&quot;;
//     readonly SUN: &quot;SUN&quot;;
//     readonly CLOUDY: &quot;CLOUDY&quot;;
// }
type ValuesOf&lt;T&gt; = T[keyof T];
const values2 = Object.values(event3) as ValuesOf&lt;typeof event2&gt;[];
// const values2: (&quot;RAIN&quot; | &quot;SUN&quot; | &quot;CLOUDY&quot;)[]

enum event3 {    
    RAIN = &quot;RAIN&quot;,
    SUN = &quot;SUN&quot;,
    CLOUDY = &quot;CLOUDY&quot;,
}
// const event3 = {
//     RAIN: &quot;RAIN&quot;,
//     SUN: &quot;SUN&quot;,
//     CLOUDY: &quot;CLOUDY&quot;,
// }
type EventType3 = event3
// ~= type EventType3 = &quot;RAIN&quot; | &quot;SUN&quot; | &quot;CLOUDY&quot;
// ~= type EventType3 = event3.RAIN | event3.SUN | event3.CLOUDY
const values3 = Object.values(event3) as event3[]

huangapple
  • 本文由 发表于 2023年6月13日 18:39:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76464041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定