在TypeScript中使用泛型时,”Separate type” 不需要翻译。

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

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://i.stack.imgur.com/aRGdR.png)

请告诉我如何将所选部分制作成单独的类型并在其他地方使用它:
简单示例: [测试播放器](https://www.typescriptlang.org/play?ssl=24&amp;ssc=70&amp;pln=24&amp;pc=1#code/KYDwDg9gTgLgBMAdgVwLZwMo2gQwObAAqAnmMAHI6rADOcA3gFACQAqjcFHALxwBEyDlD4AaFgBkIOACYZgMGAEtEeOrz4AbKbPlKVNUSwCSiRTBMAzCD36KaJs4YC+jRjFLBM2KPiIebLAA+DHCIVMAAXF64BCRklNQ0AHTsnCJw0jgwOFEA5KlQuXDBuQCC0qjKRSWlqBAwABacRS7MwfSh4VFYMX7x4cmSMnIKyqrpmdkA-FE0MFBjANoAunCt7Z3U3d6+cRQDSQ7miFYTWTlwAEYQEBrAOIhrANyujKCQsHAAxho4NHQ9HwEAASwA0ZC4TEYcDgcyyii+cAIMAAIucADwAaQQIBgSGkAJ2sQ8CVoAD4ABTuMhRTEASiiAFFcT4vjB0YDdh50h0wls4NinGTFrlJjhcqtgigNBoXiw4UpEci0dkqR5tr09qSaHSGCxmMw7vAxbN5mNiqFkDKbNLZfrmGKbFovjgNJyCElkUY8ag1WQ6S8DcwoPJkFBHo6pnAAFIYADy5CSYBwUA4FLFuqitsDzBcThhrmpnmZ8xwbOA0j2NhLrPZ7r6wB5m0iArWwtF5wlFttQA)
提前感谢您!
英文:

I have an enam and an associated union type

export enum StorageTypeNames {
User = &quot;user&quot;,
LoadSettings = &quot;loadSettings&quot;,
InitInfo = &quot;isInit&quot;,
}
type StorageType = 
| { name: StorageTypeNames.User, data: &#39;User&#39; | &#39;Admin&#39; | &#39;Amother&#39; }
| { name: StorageTypeNames.LoadSettings, data?: string[] }
| { name: StorageTypeNames.InitInfo, data: boolean };

I use this association in my class:

export class StorageHelper {
static getData&lt;K extends StorageTypeNames&gt;(type: K): **Extract&lt;StorageType, { name: K }&gt;[&#39;data&#39;] | 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&lt;StorageType, { name: K }&gt;[&#39;data&#39;] | null
//*error Cannot find name &#39;K&#39;

The problem in type variable "К"
在TypeScript中使用泛型时,”Separate type” 不需要翻译。

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&lt;K&gt; = Extract&lt;StorageType, { name: K }&gt;[&#39;data&#39;] | null

And use it in getData as ExtractedType&lt;K&gt;

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&lt;K&gt; = Extract&lt;StorageType, { name: K }&gt;[&#39;data&#39;] | null
//                ^^^ MAKE IT GENERIC

playground
That's all. Have a good day.

huangapple
  • 本文由 发表于 2023年4月10日 19:17:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976601.html
匿名

发表评论

匿名网友

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

确定