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>>, +]