question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Objects to unions converter

See original GitHub issue

Let’s say we have a dictionary:

const FEATURES = {
  SORTING: 'Sorting',
  FILTERING: 'Filtering',
  REORDERING: 'Reordering',
};

It can be used like this:

const features = [];

// or it can be a method of a class like "addFeature"
features.push(FEATURES.FILTERING);

I want to say to my features array that it can accept values of the FEATURES object only. But in order to avoid creating a separate union type and support (possible) changes there and in FEATURES object, I’d like to collect values from the object and keep them as a union type. Thus if it is necessary to add/remove value, it can be done in only one place - in the FEATURES object.

To achieve this goal, in TypeScript it is necessary to define such monstrous type:

const FEATURES = {
  SORTING: 'Sorting',
  FILTERING: 'Filtering',
  REORDERING: 'Reordering',
// 1. const assertions
} as const;

// 2. if we have a lot of such dictionaries, we have to repeat this stuff every time
type Feature = (typeof FEATURES)[keyof typeof FEATURES];

// if we want to get union type of keys
// type FeatureKeys = keyof typeof FEATURES;

const features: Feature[] = [];

// OK
features.push(FEATURES.FILTERING);

/*
  Should throw a type error
  Argument of type '"foo"' is not assignable to parameter of type
  '"Sorting" | "Filtering" | "Reordering"'.
*/
features.push('foo')

Are there any thoughts of optimizing this type generation to avoid repeating such boilerplate in Hegel? Like a help function or something.

And I’d like to know if it is a common thing to work with dictionaries this way or not. Because I’m new to strong type checking and maybe I think the wrong way here…

Thanks in advance.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thecotnecommented, Jun 3, 2020

@vasilii-kovalev i have reported Values type issue separately https://github.com/JSMonk/hegel/issues/269

1reaction
thecotnecommented, Apr 29, 2020

this works perfectly fine you don’t need to repeat anything …

const immutableId = <T>(v: T): $Immutable<T> => v

const FEATURES = immutableId({
  SORTING: 'Sorting',
  FILTERING: 'Filtering',
  REORDERING: 'Reordering'
})

type Feature = $Values<$TypeOf<FEATURES>>

const features: Array<Feature> = []

void features.push('Filtering') // ok
void features.push(FEATURES.FILTERING) // ok

void features.push('foo') // error as expected

try

you just need to use either Object.freeze or immutableId to achieve same result as as const in typescript.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript: Convert union of similar objects to object type
In TypeScript 4.1 this is a straightforward mapped type with key remapping. You can iterate over each member T of the I union...
Read more >
Type manipulations: union to tuple · Issue #13298 - GitHub
This is my "N" depth Union -> Tuple Converter that maintains the order of the ... Type-safe map over heterogeneous objects addresses this...
Read more >
Transform Union type to Tuple type in TypeScript
Let's say you have a Union , and you want to convert it to ExpectedArray. type Union = "one" | "two" | "three";...
Read more >
Documentation - Everyday Types - TypeScript
We've been using object types and union types by writing them directly in ... TypeScript only allows type assertions which convert to a...
Read more >
Union Your Data - Tableau Help
You can union your data to combine two or more tables by appending values (rows) from one table to another. To union your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found