Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Lookup #62

Closed
antfu opened this issue Aug 6, 2020 · 1 comment · Fixed by #63
Closed

Type Lookup #62

antfu opened this issue Aug 6, 2020 · 1 comment · Fixed by #63
Labels
new-challenge Propose a new challenge, a PR will be auto generated

Comments

@antfu
Copy link
Member

antfu commented Aug 6, 2020

You need to provide a detailed description of your new challenge, either in English or Chinese are acceptable.

Detailed solution/guide is not required, but be sure the challenge is solvable.

Please follow the template and fill the info. A PR will be auto-generated and always reflect on your changes.

Info

Basic info of your challenge questions,

difficulty: medium # medium / hard / extreme
title: Type Lookup
tags: union, map

Question

Sometimes, you may want to lookup for a type in a union to by their attributes.

In this challenge, I would like to have get the couponing type by searching for the common type field in the union Cat | Dog. In other words, I will expect to get Dog for LookUp<Dog | Cat, 'dog'> and Cat for LookUp<Dog | Cat, 'cat'> in the following example.

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

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

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

Template

This is the template for challengers to start the coding. Basically, you just need to change the name of your generic/function and leave to implementation any.

type LookUp<U, T> = any

Test Cases

Provide some test cases for your challenge, you can use some utils from @type-challenges/utils for asserting.

import { 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>>,
]
@antfu antfu added the new-challenge Propose a new challenge, a PR will be auto generated label Aug 6, 2020
github-actions bot pushed a commit that referenced this issue Aug 6, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Aug 6, 2020

#63 - Pull Request updated.

2020-08-06T12:26:51.831Z Preview in Playground

github-actions bot pushed a commit that referenced this issue Aug 6, 2020
@antfu antfu closed this as completed in #63 Aug 6, 2020
antfu added a commit that referenced this issue Aug 6, 2020
Co-authored-by: Anthony Fu <11247099+antfu@users.noreply.github.com>
pietrushka added a commit to pietrushka/type-challenges that referenced this issue Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-challenge Propose a new challenge, a PR will be auto generated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant