Skip to content

Instantly share code, notes, and snippets.

@nishio
Created September 17, 2021 10:29
Show Gist options
  • Save nishio/a5f8bdbc7961f3c0a569653e2dacfdd3 to your computer and use it in GitHub Desktop.
Save nishio/a5f8bdbc7961f3c0a569653e2dacfdd3 to your computer and use it in GitHub Desktop.
test("io-ts, use implemented type", () => {
const obj_string = JSON.parse(`"hello"`); // is any
const obj_number = JSON.parse(`123`); // is any
expect(isRight(t.string.decode(obj_string))).toBeTruthy();
expect(isRight(t.string.decode(obj_number))).toBeFalsy();
const ret = t.string.decode(obj_string);
let string_value: string;
if (isRight(ret)) {
string_value = ret.right;
} else {
throw new Error("not string");
}
expect(string_value).toBe("hello");
});
test("runtypes, use implemented type", () => {
const obj_string = JSON.parse(`"hello"`); // is any
const obj_number = JSON.parse(`123`); // is any
expect(String.guard(obj_string)).toBeTruthy();
expect(String.guard(obj_number)).toBeFalsy();
let string_value: string;
if (String.guard(obj_string)) {
string_value = obj_string;
} else {
throw new Error("not string");
}
expect(string_value).toBe("hello");
});
/* realworld usecase
export type TKozaneItem = {
type: "kozane";
text: string;
position: TWorldCoord;
id: ItemId;
scale: number;
custom?: {
style?: CSSProperties;
url?: string;
};
};
*/
const JSON1 = `{ "type": "kozane", "text": "", "position": [0, 0], "id": "id", "scale": 1 }`;
const JSON2 = `{ "type": "kozane", "text": "", "position": [0, 0], "id": "id" }`;
const JSON3 = `
{
"type": "kozane",
"text": "",
"position": [0, 0],
"id": "id",
"scale": 1,
"custom": { "url": "foo" }
}`;
test("io-ts, use real type", () => {
const TWorldCoord = t.tuple([t.number, t.number]);
const ItemId = t.string;
const TKozaneItem = t.intersection([
t.type({
type: t.literal("kozane"),
text: t.string,
position: TWorldCoord,
id: ItemId,
scale: t.number,
}),
t.partial({
custom: t.partial({
style: t.record(t.string, t.union([t.string, t.number])),
url: t.string,
}),
}),
]);
{
const obj = JSON.parse(JSON1); // is any
expect(isRight(TKozaneItem.decode(obj))).toBeTruthy();
}
{
const obj = JSON.parse(JSON2); // is any
expect(isRight(TKozaneItem.decode(obj))).toBeFalsy();
}
{
const obj = JSON.parse(JSON3); // is any
expect(isRight(TKozaneItem.decode(obj))).toBeTruthy();
type TKozaneItem = t.TypeOf<typeof TKozaneItem>;
let value: TKozaneItem;
if (isRight(TKozaneItem.decode(obj))) {
value = obj;
} else {
throw new Error("not TKozaneItem");
}
expect(value.custom!.url).toBe("foo");
}
});
test("runtypes, use real type", () => {
const TWorldCoord = Tuple(Number, Number);
const ItemId = String;
const key = String.check("key");
const TKozaneItem = Record({
type: Literal("kozane"),
text: String,
position: TWorldCoord,
id: ItemId,
scale: Number,
custom: Optional(
Record({
style: Optional(Record({ [key]: Union(String, Number) })),
url: Optional(String),
})
),
});
{
const obj = JSON.parse(JSON1); // is any
expect(TKozaneItem.guard(obj)).toBeTruthy();
}
{
const obj = JSON.parse(JSON2); // is any
expect(TKozaneItem.guard(obj)).toBeFalsy();
}
{
const obj = JSON.parse(JSON3); // is any
expect(TKozaneItem.guard(obj)).toBeTruthy();
type TKozaneItem = Static<typeof TKozaneItem>;
let value: TKozaneItem;
if (TKozaneItem.guard(obj)) {
value = obj;
} else {
throw new Error("not TKozaneItem");
}
expect(value.custom!.url).toBe("foo");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment