英文:
Separate type when using generics in TypeScript
问题
以下是您要翻译的内容:
我有一个名称和相关联的联合类型
export enum StorageTypeNames {
User = "user",
LoadSettings = "loadSettings",
InitInfo = "isInit",
}
type StorageType =
| { name: StorageTypeNames.User, data: 'User' | 'Admin' | 'Amother' }
| { name: StorageTypeNames.LoadSettings, data?: string[] }
| { name: StorageTypeNames.InitInfo, data: boolean };
我在我的类中使用这个关联:
export class StorageHelper {
static getData<K extends StorageTypeNames>(type: K): Extract<StorageType, { name: K }>['data'] | null;
static getData(type: StorageTypeNames) {
let data: string | null = null;
data = localStorage.getItem(type);
return data ? JSON.parse(data) : null;
}
}
我想将选择的(加粗)部分移到一个单独的类型中,但是存在与泛型 K 有关的问题
当尝试将其移到单独的类型时,我收到错误:
type ExtractedType = Extract<StorageType, { name: K }>['data'] | null
//*错误 无法找到名称 'K'
问题出在类型变量 "K"
[](https://i.stack.imgur.com/aRGdR.png)
请告诉我如何将所选部分制作成单独的类型并在其他地方使用它:
简单示例: [测试播放器](https://www.typescriptlang.org/play?ssl=24&ssc=70&pln=24&pc=1#code/KYDwDg9gTgLgBMAdgVwLZwMo2gQwObAAqAnmMAHI6rADOcA3gFACQAqjcFHALxwBEyDlD4AaFgBkIOACYZgMGAEtEeOrz4AbKbPlKVNUSwCSiRTBMAzCD36KaJs4YC+jRjFLBM2KPiIebLAA+DHCIVMAAXF64BCRklNQ0AHTsnCJw0jgwOFEA5KlQuXDBuQCC0qjKRSWlqBAwABacRS7MwfSh4VFYMX7x4cmSMnIKyqrpmdkA-FE0MFBjANoAunCt7Z3U3d6+cRQDSQ7miFYTWTlwAEYQEBrAOIhrANyujKCQsHAAxho4NHQ9HwEAASwA0ZC4TEYcDgcyyii+cAIMAAIucADwAaQQIBgSGkAJ2sQ8CVoAD4ABTuMhRTEASiiAFFcT4vjB0YDdh50h0wls4NinGTFrlJjhcqtgigNBoXiw4UpEci0dkqR5tr09qSaHSGCxmMw7vAxbN5mNiqFkDKbNLZfrmGKbFovjgNJyCElkUY8ag1WQ6S8DcwoPJkFBHo6pnAAFIYADy5CSYBwUA4FLFuqitsDzBcThhrmpnmZ8xwbOA0j2NhLrPZ7r6wB5m0iArWwtF5wlFttQA)
提前感谢您!
英文:
I have an enam and an associated union type
export enum StorageTypeNames {
User = "user",
LoadSettings = "loadSettings",
InitInfo = "isInit",
}
type StorageType =
| { name: StorageTypeNames.User, data: 'User' | 'Admin' | 'Amother' }
| { name: StorageTypeNames.LoadSettings, data?: string[] }
| { name: StorageTypeNames.InitInfo, data: boolean };
I use this association in my class:
export class StorageHelper {
static getData<K extends StorageTypeNames>(type: K): **Extract<StorageType, { name: K }>['data'] | null;**
static getData(type: StorageTypeNames) {
let data: string | null = null;
data = localStorage.getItem(type);
return data ? JSON.parse(data) : null;
}
}
I want to move the selected(boldet) part to a separate type, but there is a problem with generic K
When trying to move to a separate type, i get error:
type ExtractedType = Extract<StorageType, { name: K }>['data'] | null
//*error Cannot find name 'K'
The problem in type variable "К"
please tell me how to make the selected part into a separate type and use it in other places:
simple ex.: test playground
thank you in advance!
答案1
得分: 1
ExtractedType
中的 K
是一个泛型类型参数。所以你需要创建一个泛型类型来将 K
的值传递给 ExtractedType
。
type ExtractedType<K> = Extract<StorageType, { name: K }>['data'] | null
然后在 getData
中使用它,如 ExtractedType<K>
。
查看泛型文档获取更多信息。
英文:
K
in ExtractedType
is a generic type parameter. So you need to create a generic type to pass that K
value to the ExtractedType
.
type ExtractedType<K> = Extract<StorageType, { name: K }>['data'] | null
And use it in getData
as ExtractedType<K>
Take a look at Generics Documentation for more info
答案2
得分: 0
类型ExtractedType<K> = Extract<StorageType, { name: K }>['data'] | null
// ^^^ 使它成为泛型
playground
这就是全部了。祝你有一个美好的一天。
<details>
<summary>英文:</summary>
You are trying to make a generic type, but you forgot to make it generic
```ts
type ExtractedType<K> = Extract<StorageType, { name: K }>['data'] | null
// ^^^ MAKE IT GENERIC
playground
That's all. Have a good day.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论