How to use the runtypes.Dictionary function in runtypes

To help you get started, we’ve selected a few runtypes examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pelotom / runtypes / examples / src / space-game.ts View on Github external
location: Vector,
  mass: NonNegative,
  name: String,
  crew: Array(CrewMember),
});
type Ship = Static<
  typeof Ship
>; /* = {
  type: 'ship';
  location: Vector;
  mass: NonNegative;
  name: string;
  crew: CrewMember[];
}*/

const Fleet = Dictionary(Ship, 'number');
type Fleet = Static; // = { [_: number]: Ship }

const SpaceObject = Union(Asteroid, Planet, Ship);
type SpaceObject = Static; // = Asteroid | Planet | Ship

const isHabitable = SpaceObject.match(asteroid => false, planet => planet.habitable, ship => true);

// Pattern matching from a union
function foo(spaceObject: SpaceObject) {
  if (isHabitable(spaceObject)) {
    // ...
  }
}
github citycide / trilogy / src / types.ts View on Github external
export type CriteriaListNormalized  =
  CriteriaBaseNormalized>[]

export type Criteria  =
  | CriteriaBase
  | CriteriaList

export type CriteriaNormalized  =
  | CriteriaBaseNormalized
  | CriteriaListNormalized

export const Index = t.Union(
  t.String,
  t.Array(t.Union(t.String, t.Array(t.String))),
  t.Dictionary(t.Union(t.String, t.Array(t.String)))
)

export const GroupClause = t.Union(
  t.String,
  t.Array(t.String)
)

export const OrderClause = t.Union(
  t.String,
  t.Tuple(t.String, t.String)
)

export const TrilogyOptions = t.Partial({
  client: t.Union(t.Literal('sqlite3'), t.Literal('sql.js')),
  dir: t.String
})