跳至主要內容

62. Type Lookup

鸭梨小于 1 分钟

62. Type Lookup

<题目描述>

::: playground#ts 62. Type Lookup

@file index.ts

// ============= Test Cases =============
import type { Equal, Expect } from '@type-challenges/utils'

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

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

type Animal = Cat | Dog

type cases = [
  Expect<Equal<LookUp<Animal, 'dog'>, Dog>>,
  Expect<Equal<LookUp<Animal, 'cat'>, Cat>>,
]

// ============= Your Code Here =============
type LookUp<U, T> = any

:::

点击查看答案
type LookUp<U, T> = U extends { type: T } ? U : never