-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(question): add #17908 - Union from values in an object
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
questions/17908-medium-union-from-values-in-an-object/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Create a generalizable type UnionOfArrayValuesInObject that can be used to create a union type of all the values from all the array values inside an object that contains object keys and arrays as the objects. | ||
|
||
The task is solvable without using utility types. | ||
|
||
```ts | ||
const object1WithObjectArrays= { | ||
key: ['one', 'two', 'three'] | ||
} as const | ||
|
||
const object2WithObjectArrays= { | ||
key: [1, 2, 3] | ||
} as const | ||
|
||
const AllMeasurementUnits = { | ||
weight: ['g', 'kg', 'pound'], | ||
length: ['cm', 'm', 'inch'] | ||
} as const | ||
|
||
type UnionOfArrayValuesInObject<T> = any | ||
|
||
const oneString: UnionOfArrayValuesInObject<typeof object1WithObjectArrays> = 'one' //should be a valid type | ||
const fourString: UnionOfArrayValuesInObject<typeof object2WithObjectArrays> = 'four' //shouldn't be a valid type | ||
|
||
const oneNumber: UnionOfArrayValuesInObject<typeof object2WithObjectArrays> = 1 //should be a valid type | ||
|
||
const grams: UnionOfArrayValuesInObject<typeof AllMeasurementUnits> = 'g' //should be a valid type | ||
const ounce: UnionOfArrayValuesInObject<typeof AllMeasurementUnits> = 'ounce' //shouldn't be a valid type | ||
``` |
7 changes: 7 additions & 0 deletions
7
questions/17908-medium-union-from-values-in-an-object/info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
difficulty: medium | ||
title: Union from values in an object | ||
tags: union, array, object, object-keys, const assertions | ||
author: | ||
github: koodiohto | ||
name: Ohto Rainio | ||
|
18 changes: 18 additions & 0 deletions
18
questions/17908-medium-union-from-values-in-an-object/template.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const object1WithObjectArrays= { | ||
key: ['one', 'two', 'three'] | ||
} as const | ||
|
||
const object2WithObjectArrays= { | ||
key: [1, 2, 3] | ||
} as const | ||
|
||
const AllMeasurementUnits = { | ||
weight: ['g', 'kg', 'pound'], | ||
length: ['cm', 'm', 'inch'] | ||
} as const | ||
|
||
type UnionOfArrayValuesInObject<T> = any | ||
|
||
const oneString: UnionOfArrayValuesInObject<typeof object1WithObjectArrays> = 'one' //should be a valid type | ||
const oneNumber: UnionOfArrayValuesInObject<typeof object2WithObjectArrays> = 1 //should be a valid type | ||
const grams: UnionOfArrayValuesInObject<typeof AllMeasurementUnits> = 'g' //should be a valid type |
7 changes: 7 additions & 0 deletions
7
questions/17908-medium-union-from-values-in-an-object/test-cases.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Equal, Expect } from '@type-challenges/utils' | ||
|
||
type cases = [ | ||
Expect<Equal<UnionOfArrayValuesInObject<typeof object1WithObjectArrays>, 'one' | 'two' | 'three'>>, | ||
Expect<Equal<UnionOfArrayValuesInObject<typeof object2WithObjectArrays>, 1 | 2 | 3 >>, | ||
Expect<Equal<UnionOfArrayValuesInObject<typeof AllMeasurementUnits>, 'g' | 'kg' | 'pound' | 'cm' | 'm' | 'inch'>>, | ||
] |