1 Star 0 Fork 0

Sunny / type-challenges

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.ja.md 2.24 KB
AI 代码解读
一键复制 编辑 原始数据 按行查看 历史
antfu 提交于 2022-07-02 00:04 . chore: update index

Type Lookup 中級 #union #map

by Anthony Fu @antfu

挑戦する    English 简体中文 한국어

Union 型から特定の型を属性を使って取得したいことがあります。

この課題では、Cat | Dog という Union 型に共通する type というフィールドを使って、対応する型を取得します。つまり、以下の例のように、 LookUp<Dog | Cat, 'dog'> の場合は Dog を、LookUp<Dog | Cat, 'cat'> の場合は Cat を取得することになります。

interface Cat {
  type: 'cat'
  breeds: 'Abyssinian' | 'Shorthair' | 'Curl' | 'Bengal'
}

interface Dog {
  type: 'dog'
  breeds: 'Hound' | 'Brittany' | 'Bulldog' | 'Boxer'
  color: 'brown' | 'white' | 'black'
}

type MyDog = LookUp<Cat | Dog, 'dog'> // expected to be `Dog`

戻る 解答を共有 解答を確認
1
https://gitee.com/ztes/type-challenges.git
git@gitee.com:ztes/type-challenges.git
ztes
type-challenges
type-challenges
main

搜索帮助