From 00f824b5bd66ec7d15b4e2927ba12e4e4f7f987f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 6 Aug 2020 20:50:44 +0800 Subject: [PATCH] feat(question): add #62 - Type Lookup (#63) Co-authored-by: Anthony Fu <11247099+antfu@users.noreply.github.com> --- questions/62-medium-type-lookup/README.md | 18 ++++++++++++++++++ questions/62-medium-type-lookup/info.yml | 7 +++++++ questions/62-medium-type-lookup/template.ts | 1 + questions/62-medium-type-lookup/test-cases.ts | 19 +++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 questions/62-medium-type-lookup/README.md create mode 100644 questions/62-medium-type-lookup/info.yml create mode 100644 questions/62-medium-type-lookup/template.ts create mode 100644 questions/62-medium-type-lookup/test-cases.ts diff --git a/questions/62-medium-type-lookup/README.md b/questions/62-medium-type-lookup/README.md new file mode 100644 index 00000000..faa7105b --- /dev/null +++ b/questions/62-medium-type-lookup/README.md @@ -0,0 +1,18 @@ +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` and `Cat` for `LookUp` in the following example. + +```ts +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 // expected to be `Dog` +``` diff --git a/questions/62-medium-type-lookup/info.yml b/questions/62-medium-type-lookup/info.yml new file mode 100644 index 00000000..3c2b7bc0 --- /dev/null +++ b/questions/62-medium-type-lookup/info.yml @@ -0,0 +1,7 @@ +difficulty: medium +title: Type Lookup +tags: 'union, map' +author: + github: antfu + name: Anthony Fu + diff --git a/questions/62-medium-type-lookup/template.ts b/questions/62-medium-type-lookup/template.ts new file mode 100644 index 00000000..af722474 --- /dev/null +++ b/questions/62-medium-type-lookup/template.ts @@ -0,0 +1 @@ +type LookUp = any diff --git a/questions/62-medium-type-lookup/test-cases.ts b/questions/62-medium-type-lookup/test-cases.ts new file mode 100644 index 00000000..0d9c5c2e --- /dev/null +++ b/questions/62-medium-type-lookup/test-cases.ts @@ -0,0 +1,19 @@ +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, Dog>>, + Expect, Cat>>, +]