8000 Extracting union of strings to runtime array? · Issue #38 · runtypes/runtypes · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Extracting union of strings to runtime array? #38
Open
@danielkcz

Description

@danielkcz

I am curious if that's possible somehow? You see, I am using library mobx-state-tree which is also checked at runtime, but currently, it's impossible to use Typescript types for modeling.

type State = 'Unknown' | 'Online' | 'Offline'

const Model = types.model({
  connState: types.enumeration(['Unknown', 'Online', 'Offline'])
})

The first step should be probably to support Symbol.iterator and then I can do this.

const State = Union(
  Literal('Unknown'),
  Literal('Online'),
  Literal('Offline')
)

const Model = types.model({
  connState: types.enumeration(Array.from(State))
})

However, that's not enough since it would give an array of Literal objects. I am not how to proceed there to keep the ease of use. Perhaps some custom method like State.toArray() could be more feasible?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0