Skip to content

Instantly share code, notes, and snippets.

@Daiz
Created August 2, 2018 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daiz/db1b5864ffc2db4821131dbf819c6d85 to your computer and use it in GitHub Desktop.
Save Daiz/db1b5864ffc2db4821131dbf819c6d85 to your computer and use it in GitHub Desktop.
interface MatterConfig {
foo: boolean;
}
interface Example {
foo: string;
bar: number;
}
type CombinedConfig = Example & MatterConfig;
type TestAlignment = string | boolean;
type Alignment = 1 | 2 | 3 | 4 | TestAlignment | 5 | "none";
type NumberList = number[];
type NumberMapper = Mapper<NumberList>;
function isCombinedConfig(x: any): x is CombinedConfig {
if (x == null) return false;
if (!(isExample(x) && isMatterConfig(x))) return false;
return true;
}
function isNumberList(x: any): x is NumberList {
if (!(x instanceof Array)) return false;
for (let i = 0; i < x.length; ++i) {
if (typeof x[i] !== NUMBER) return false;
}
return true;
}
function testing(x: any) {
if (isMapper(x, isNumberList)) {
const test = x.map[0];
return test + "2o";
}
}
interface Mapper<T> {
map: T;
name: string;
}
function isMapper<T>(x: any, isT: (x: any) => x is T): x is Mapper<T> {
if (x == null) return false;
if (!isT(x.map)) return false;
if (typeof x.name !== STRING) return false;
return true;
}
interface Tester {
align: Alignment;
constant: StringEnum;
}
enum StringEnum {
"Foo",
"Bar",
"Baz"
}
enum NumberEnum {
Foo,
Bar,
Baz
}
function isAnyOf(value: any, array: any[]): boolean {
for (let i = 0; i < array.length; ++i) {
if (array[i] === value) return true;
}
return false;
}
const valuesOfAlignment = [1, 2, 3, 4, 5, "none"];
const valuesOfStringEnum = [StringEnum.Foo, StringEnum.Bar, StringEnum.Baz];
function isTester(x: any): x is Tester {
if (
!(
isAnyOf(x.align, valuesOfAlignment) ||
typeof x.align === STRING ||
typeof x.align === BOOLEAN
)
)
return false;
if (!isAnyOf(x.enum, valuesOfStringEnum)) return false;
return true;
}
interface EngineConfig {
pathPlanner: PathPlannerConfig;
debug: DebugConfig;
matter: string | number | MatterConfig;
tupleArray: [number, string][];
interfaceArray?: Example[];
}
interface PathPlannerConfig {
nbMaxIter: number;
nbIterPerChunk: number;
heuristic?: string;
}
interface DebugConfig {
logLevel: number;
}
const OBJECT = "object";
const STRING = "string";
const NUMBER = "number";
const BOOLEAN = "boolean";
const UNDEFINED = "undefined";
function isNumber(x: any): x is number {
return typeof x === NUMBER;
}
function isString(x: any): x is string {
return typeof x === STRING;
}
function isBoolean(x: any): x is boolean {
return typeof x === BOOLEAN;
}
function isObject(x: any): x is object {
return typeof x === OBJECT;
}
function isArray(x: any): x is Array<any> {
return x instanceof Array;
}
function isExample(x: any): x is Example {
if (x == null) return false;
if (typeof x.foo !== STRING) return false;
if (typeof x.bar !== NUMBER) return false;
return true;
}
function isNumberStringTuple(x: any): x is [number, string] {
if (!(x instanceof Array)) return false;
if (typeof x[0] !== NUMBER) return false;
if (typeof x[1] !== STRING) return false;
if (x.length !== 2) return false;
return true;
}
function isMatterConfig(x: any): x is MatterConfig {
if (x == null) return false;
if (typeof x.foo !== BOOLEAN) return false;
return true;
}
function isDebugConfig(x: any): x is DebugConfig {
if (x == null) return false;
if (typeof x.logLevel !== NUMBER) return false;
return true;
}
function isPathPlannerConfig(x: any): x is PathPlannerConfig {
if (x == null) return false;
if (typeof x.nbMaxIter !== NUMBER) return false;
if (typeof x.nbIterPerChunk !== NUMBER) return false;
if (!(x.heuristic === undefined || typeof x.heuristic === STRING))
return false;
return true;
}
function isEngineConfig(x: any): x is EngineConfig {
if (x == null) return false;
if (!isPathPlannerConfig(x.pathPlanner)) return false;
if (!isDebugConfig(x.debug)) return false;
const matterType = typeof x.matter;
if (
!(
matterType === STRING ||
matterType === NUMBER ||
isMatterConfig(x.matter)
)
)
return false;
if (!(x.tupleArray instanceof Array)) return false;
for (let i = 0; i < x.tupleArray.length; ++i) {
if (!isNumberStringTuple(x.tupleArray[i])) return false;
}
if (!(x.interfaceArray === undefined || x.interfaceArray instanceof Array))
return false;
if (x.interfaceArray) {
for (let i = 0; i < x.interfaceArray.length; ++i) {
if (!isExample(x.interfaceArray[i])) return false;
}
}
return true;
}
function loadConfig(x: any) {
if (isEngineConfig(x)) {
x.debug.logLevel = 3;
x.pathPlanner.heuristic = "yes";
x.pathPlanner.heuristic = null;
} else {
// x failed to type check for EngineConfig
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment