![](/img/trans.png)
[英]Is it possible to create a discriminated union using mapped types in TypeScript?
[英]TypeScript: is it possible to create a mapped union type?
是否可以組合“映射類型”和“聯合類型”功能來創建一個表達式,該表達式將以下接口作為輸入:
interface AwaActionTypes {
CLICKLEFT: 'CL';
CLICKRIGHT: 'CR';
SCROLL: 'S';
ZOOM: 'Z';
RESIZE: 'R';
KEYBOARDENTER: 'KE';
KEYBOARDSPACE: 'KS';
OTHER: 'O';
}
並生成一個類似於以下union類型別名的類型:
type AwaActionType: 'CL' | 'CR' | 'S' | 'Z' | 'R' | 'KE' | 'KS' | 'O';
我嘗試使用keyof
, |
組合 等等沒有降落在有效的東西上。 手冊中也沒有看到任何內容。
它是keyof和lookup類型的組合
type AwaActionType = AwaActionTypes[keyof AwaActionTypes];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.