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.

Suggestion: require value property to avoid collisions when nesting

See original GitHub issue

When a union is nested inside a union, without a value property specified, the two values will collide:

E.g.

https://stackblitz.com/edit/typescript-ef4lbo?file=index.ts

import { unionize, ofType } from 'unionize';

const InsideFoo = unionize({
  Bar: ofType<{ bar: number }>(),
});
type InsideFoo = typeof InsideFoo._Union;
const A = unionize({
  Foo: ofType<InsideFoo>(),
});

const a = A.Foo(InsideFoo.Bar({ bar: 1 }));

console.log(JSON.stringify(a))

Logs:

{"bar":1,"tag":"Foo"}

As you can see, the tag for the nested union is lost, and the value for the nested union is merged with the value for the parent union.

We can fix this by specifying a value property:

import { unionize, ofType } from 'unionize';

const InsideFoo = unionize({
  Bar: ofType<{ bar: number }>(),
}, { value: 'value' });
type InsideFoo = typeof InsideFoo._Union;
const A = unionize({
  Foo: ofType<InsideFoo>(),
}, { value: 'value' });

const a = A.Foo(InsideFoo.Bar({ bar: 1 }));

console.log(JSON.stringify(a))

Logs:

{"tag":"Foo","value":{"tag":"Bar","value":{"bar":1}}}

For this reason, I think that the value property should be required. I always forget to add one, and then run into these issues. Ideally I wouldn’t have to specify a value property each time I use unionize.

Related code: https://github.com/pelotom/unionize/blob/f6ebfa50177aa3fdb724378c158e400c72d0d625/src/index.ts#L115

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
OliverJAshcommented, Jun 18, 2019

You’re right. Amazing! Nice one @karol-majewski

0reactions
pelotomcommented, Jun 18, 2019

@OliverJAsh this no longer type checks since #56:

const InsideFoo = unionize({
  Bar: ofType<{ bar: number }>(),
});
type InsideFoo = typeof InsideFoo._Union;
const A = unionize({
  Foo: ofType<InsideFoo>(),
});

You are forced to specify either a) a tag prop other than "tag" for InsideFoo or b) a value prop for A.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use javascript proxy for nested objects - Stack Overflow
You may want to change the name of isProxy to avoid naming collisions with properties of stored objects. Note: the nested proxy is...
Read more >
Suggested Practices for Avian Protection on Power Lines.
required for nest management, carcass salvage, or other bird management purposes. BIOLOGICAL ASPECTS OF. AVIAN ELECTROCUTION. Bird electrocutions on power ...
Read more >
Optional chaining '?.' - The Modern JavaScript Tutorial
The optional chaining ?. is a safe way to access nested object properties, even if an intermediate property doesn't exist.
Read more >
CSS Cascading and Inheritance Level 5 - W3C
The cascade takes an unordered list of declared values for a given property on a given element, sorts them by their declaration's precedence...
Read more >
JAXB Users Guide - Java EE
If the collision is coming from two different schemas with different target namespaces, then you can easily avoid the collision by compiling them...
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