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<typeof Fleet>; // = { [_: number]: Ship }

const SpaceObject = Union(Asteroid, Planet, Ship);
type SpaceObject = Static<typeof SpaceObject>; // = 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 <D = LooseObject> =
  CriteriaBaseNormalized<DistinctArrayTuple<D>>[]

export type Criteria <D = LooseObject> =
  | CriteriaBase<D>
  | CriteriaList<D>

export type CriteriaNormalized <D = LooseObject> =
  | CriteriaBaseNormalized<D>
  | CriteriaListNormalized<D>

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
})