Table of contents
import * as io from "io-ts";
import { AnyPrintable, SelectionOf, table, RowOf, RowsArray } from "../../src";
const t = table(["a", "b"], "t");
const q = t.selectStar();
Getting Response Keys
type K = SelectionOf<typeof q>; // typeof K = 'a' | 'b'
// @ts-expect-error
const k2: K = "c";
Response Object
type R1 = RowOf<typeof q>; // typeof Ret = {a: string | number | null | undefined, b: string | number | null | undefined, }
const ret1: R1 = { a: 1, b: null };
//@ts-expect-error
ret1.c;
Response Array
type R2 = RowsArray<typeof q>; // typeof Ret = {a: string | number | null | undefined, b: string | number | null | undefined, }[]
const ret2: R2 = [] as any;
//@ts-expect-error
ret2?.[0]?.abc;
Usage with io-ts
const ioTsResponse = <
T extends AnyPrintable,
C extends { [key in SelectionOf<T>]: io.Mixed }
>(
_it: T,
_codec: C
): Promise<io.TypeOf<io.TypeC<C>>[]> => {
// Get the query string with it.stringify()
// and implement the DB comms.
return Promise.resolve([]);
};
const response = await ioTsResponse(t.selectStar(), {
a: io.string,
b: io.number,
});
response[0]?.a.charAt(0);
response[0]?.b.toPrecision(2);
//@ts-expect-error
response[0]?.c;
// @ts-expect-error
ioTsResponse(t.selectStar(), { a: io.string });
This document used eval-md