Skip to content

Tags are not removed from variant value #67

@OliverJAsh

Description

@OliverJAsh
import { ofType, unionize } from 'unionize';

const MyUnion = unionize({
    MyVariant: ofType<{ foo: string }>(),
});
type MyUnion = typeof MyUnion._Union;

const myUnion = MyUnion.MyVariant({ foo: 'bar' });
MyUnion.match(myUnion, {
    // Type: { foo: 'bar' }
    // Expected logged value: { foo: 'bar' } (same as type)
    // Actual logged value: { tag: 'MyVariant', foo: 'bar' }
    MyVariant: myVariant => console.log(myVariant),
});

Workaround: if we specify a value, we don't have this issue:

const MyUnion = unionize(
    {
        MyVariant: ofType<{ foo: string }>(),
    },
    { value: 'value' },
);

Why this matters: we are spreading the unionize variant value into a React component:

<div {...myVariant} />

We expect the spreaded object to contain valid props for this React component, which it does, however it also passes the tag prop which is not valid. This results in invalid HTML.

IMO the runtime value should match the type. There should not be an excess property containing the tag—although I realise this would mean creating a new object, in order to remove the tag

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions