Skip to content

Commit

Permalink
feat(question): add #17908 - Union from values in an object
Browse files Browse the repository at this point in the history
  • Loading branch information
koodiohto committed Nov 14, 2022
1 parent a3a4910 commit e701812
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
28 changes: 28 additions & 0 deletions questions/17908-medium-union-from-values-in-an-object/README.md
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
```
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 questions/17908-medium-union-from-values-in-an-object/template.ts
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
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'>>,
]

0 comments on commit e701812

Please sign in to comment.