8000 Reconsider duck typing records · Issue #2 · typed-immutable/typed-immutable · GitHub
[go: up one dir, main page]

< 8000 /div>
Skip to content

Reconsider duck typing records #2

@Gozala

Description

@Gozala

At the moment if you define types with a following signatures:

const X = Record({x: 0}, 'X')
const Y = Record({y: 0}, 'Y')

const C = Record({ data: Union(X, Y) })

Then type union interprets data value not always as one would expected:

C({data: {x: 2}}) // => Typed.Record({data: Union(X, Y)})({ "data": X({ "x": 2 }) })
C({data: X({x: 5})}) // => Typed.Record({data: Union(X, Y)})({ "data": X({ "x": 5 }) })
C({data: Y({y: 3})}) // => Typed.Record({data: Union(X, Y)})({ "data": Y({ "y": 3 }) })

C({data: {y: 2}}) // => Typed.Record({data: Union(X, Y)})({ "data": X({ "x": 0 }) })

Most likely in last statement data field was expected to be an instance of Y instead of X, although given that {y: 2} can be read both as X and Y it was interpreted as a first type in the union X.

It maybe better to disallow passing untyped values all together in order to avoid this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0