Suggestion: require value property to avoid collisions when nesting
See original GitHub issueWhen 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:
- Created 5 years ago
- Comments:14 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You’re right. Amazing! Nice one @karol-majewski
@OliverJAsh this no longer type checks since #56:
You are forced to specify either a) a
tag
prop other than"tag"
forInsideFoo
or b) avalue
prop forA
.