我需要從聯合中過濾 Map 型別
type B = FilterMap<string | { a: 1 }> // { a: 1 }
type C = FilterMap<boolean | { b:2 }> // { b:2 }
type D = FilterMap<number | { c: 3 }> // { c: 3 }
這可能嗎?
uj5u.com熱心網友回復:
您可以使用預定義的Exatrcttype 從聯合中提取任何物件型別:
type FilterMap<T> = Extract<T, object>
type B = FilterMap<string | { a: 1 }> // { a: 1 }
type C = FilterMap<boolean | { b:2 }> // { b:2 }
type D = FilterMap<number | { c: 3 }> // { c: 3 }
游樂場鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/433059.html
標籤:打字稿
上一篇:是否可以在打字稿中選擇宣告型別?
