How to use runtypes - 10 common examples

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 Hypercubed / dynamo / tests / types-runtypes.spec.ts View on Github external
// tslint:disable-next-line:variable-name
const Name = convertConstraint(nameConstraint);
type Name = InstanceType<typeof Name>;

const ageConstraint = NumberType.withConstraint(n => {
  return Number.isInteger(n) && n >= 0;
});

// tslint:disable-next-line:variable-name
const Age = convertConstraint(ageConstraint);
type Age = InstanceType<typeof Age>;

const personRecord = Record({
  name: nameConstraint,
  age: ageConstraint,
  kind: Literal('$person')
});
// tslint:disable-next-line:variable-name
const Person = convertConstraint(personRecord);
type Person = Static<typeof personRecord>;

dynamo.add(Name, Age, Person);

class CreatePerson {
  @signature()
  person(name: Name, age: Age): Person {
    return { name, age, kind: '$person' };
  }
}

const createPerson = dynamo.function(CreatePerson);
github Hypercubed / dynamo / tests / types-runtypes.spec.ts View on Github external
constraint['name'] = name ? `${name} ${constraint['tag']}` : constraint['tag'];
  guard()(constraint, 'guard');
  return constraint as any;
}

const dynamo = new Dynamo();

const nameConstraint = StringType.withConstraint(x => {
  return x.length > 0;
});

// tslint:disable-next-line:variable-name
const Name = convertConstraint(nameConstraint);
type Name = InstanceType<typeof Name>;

const ageConstraint = NumberType.withConstraint(n => {
  return Number.isInteger(n) && n >= 0;
});

// tslint:disable-next-line:variable-name
const Age = convertConstraint(ageConstraint);
type Age = InstanceType<typeof Age>;

const personRecord = Record({
  name: nameConstraint,
  age: ageConstraint,
  kind: Literal('$person')
});
// tslint:disable-next-line:variable-name
const Person = convertConstraint(personRecord);
type Person = Static<typeof personRecord>;
github Hypercubed / dynamo / tests / types-runtypes.spec.ts View on Github external
import { assert, IsExact } from 'conditional-type-checks';

interface ConstraintToken<V> extends Constraint<any> {
  name: string;
  new(...args: any[]): V;
}

function convertConstraint<T extends Runtype<unknown>>(constraint: T, name?: string): ConstraintToken<Static<T>> {
  constraint['name'] = name ? `${name} ${constraint['tag']}` : constraint['tag'];
  guard()(constraint, 'guard');
  return constraint as any;
}

const dynamo = new Dynamo();

const nameConstraint = StringType.withConstraint(x => {
  return x.length > 0;
});

// tslint:disable-next-line:variable-name
const Name = convertConstraint(nameConstraint);
type Name = InstanceType<typeof Name>;

const ageConstraint = NumberType.withConstraint(n => {
  return Number.isInteger(n) && n >= 0;
});

// tslint:disable-next-line:variable-name
const Age = convertConstraint(ageConstraint);
type Age = InstanceType<typeof Age>;

const personRecord = Record({
github pelotom / runtypes / examples / src / union-values.ts View on Github external
import { Static, Union, Literal, match } from 'runtypes';

// Define the runtype
const Day = Union(
  Literal('Sunday'),
  Literal('Monday'),
  Literal('Tuesday'),
  Literal('Wednesday'),
  Literal('Thursday'),
  Literal('Friday'),
  Literal('Saturday'),
);

// Extract the static type
type Day = Static<typeof Day>; // = 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday'

// Extract enumerated literal values
const days: Day[] = Day.alternatives.map(lit => lit.value);

for (const day of days) {
  console.log(`Good morning, it's ${day}!`);
}
github pelotom / runtypes / examples / src / union-values.ts View on Github external
Literal('Thursday'),
  Literal('Friday'),
  Literal('Saturday'),
);

// Extract the static type
type Day = Static<typeof Day>; // = 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday'

// Extract enumerated literal values
const days: Day[] = Day.alternatives.map(lit => lit.value);

for (const day of days) {
  console.log(`Good morning, it's ${day}!`);
}

const isWeekend = match(
  [Literal('Sunday'), () => true],
  [Literal('Saturday'), () => true],
  [Day, () => false],
);
github pelotom / runtypes / examples / src / space-game.ts View on Github external
});
type Planet = Static<
  typeof Planet
>; /* = {
  type: 'planet';
  location: Vector;
  mass: NonNegative;
  population: NonNegative;
  habitable: Boolean;
}*/

const Rank = Union(
  Literal('captain'),
  Literal('first mate'),
  Literal('officer'),
  Literal('ensign'),
);
type Rank = Static<typeof Rank>; // = 'captain' | 'first mate' | 'officer' | 'ensign'

const CrewMember = Record({
  name: String,
  age: NonNegative,
  rank: Rank,
  home: Planet,
});
type CrewMember = Static<
  typeof CrewMember
>; /* = {
  name: string;
  age: NonNegative;
  rank: Rank;
  home: Planet;
github pelotom / runtypes / examples / src / space-game.ts View on Github external
const CrewMember = Record({
  name: String,
  age: NonNegative,
  rank: Rank,
  home: Planet,
});
type CrewMember = Static<
  typeof CrewMember
>; /* = {
  name: string;
  age: NonNegative;
  rank: Rank;
  home: Planet;
}*/

const Ship = Record({
  type: Literal('ship'),
  location: Vector,
  mass: NonNegative,
  name: String,
  crew: Array(CrewMember),
});
type Ship = Static<
  typeof Ship
>; /* = {
  type: 'ship';
  location: Vector;
  mass: NonNegative;
  name: string;
  crew: CrewMember[];
}*/
github jschr / vscodethemes / types / runtime.ts View on Github external
trendingDaily: Number,
  trendingWeekly: Number,
  trendingMonthly: Number,
}).And(
  // Optional properties.
  Partial({
    displayName: String.Or(Null),
    shortDescription: String.Or(Null),
    repositoryUrl: String.Or(Null),
  }),
)

export const PackageJSONRuntime = Record({
  contributes: Record({
    themes: Array(
      Record({
        path: String,
      }).And(
        Partial({
          label: String,
          uiTheme: String,
        }),
      ),
    ),
  }),
})

export const ThemeTypeRuntime = Union(
  Literal('light'),
  Literal('dark'),
  Literal('hc'),
)
github pelotom / runtypes / examples / src / union-values.ts View on Github external
import { Static, Union, Literal, match } from 'runtypes';

// Define the runtype
const Day = Union(
  Literal('Sunday'),
  Literal('Monday'),
  Literal('Tuesday'),
  Literal('Wednesday'),
  Literal('Thursday'),
  Literal('Friday'),
  Literal('Saturday'),
);

// Extract the static type
type Day = Static<typeof Day>; // = 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday'

// Extract enumerated literal values
const days: Day[] = Day.alternatives.map(lit => lit.value);

for (const day of days) {
github jschr / vscodethemes / types / runtime.ts View on Github external
export const ColorsRuntime = Partial({
  // These colors may not exist in the object but also may be null.
  activityBarBackground: String.Or(Null),
  activityBarForeground: String.Or(Null),
  activityBarBorder: String.Or(Null),
  statusBarBackground: String.Or(Null),
  statusBarForeground: String.Or(Null),
  editorBackground: String.Or(Null),
  editorForeground: String.Or(Null),
  editorGroupHeaderTabsBackground: String.Or(Null),
  editorLineNumberForeground: String.Or(Null),
  tabActiveBackground: String.Or(Null),
  tabActiveForeground: String.Or(Null),
  tabInactiveBackground: String.Or(Null),
  tabInactiveForeground: String.Or(Null),
  editorGroupHeaderTabsBorder: String.Or(Null),
  tabActiveBorder: String.Or(Null),
  tabBorder: String.Or(Null),
  contrastActiveBorder: String.Or(Null),
  contrastBorder: String.Or(Null),
  titleBarActiveBackground: String.Or(Null),
  titleBarActiveForeground: String.Or(Null),
  titleBarInactiveBackground: String.Or(Null),
  titleBarInactiveForeground: String.Or(Null),
  titleBarBorder: String.Or(Null),
})

export const LineTokenRuntime = Record({
  token: String,
  style: Partial({
    color: String,
    fontWeight: String,