structured-outputs 0.1.0-beta.23 → 0.1.0-beta.24
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +2 -2
- package/esm/types/Ty.d.ts +5 -4
- package/esm/types/Ty.d.ts.map +1 -1
- package/esm/types/Ty.js +2 -2
- package/esm/types/Ty.js.map +1 -1
- package/esm/types/constantUnion.d.ts.map +1 -1
- package/esm/types/constantUnion.js +1 -0
- package/esm/types/constantUnion.js.map +1 -1
- package/esm/types/primitives.d.ts +0 -1
- package/esm/types/primitives.d.ts.map +1 -1
- package/esm/types/primitives.js +3 -12
- package/esm/types/primitives.js.map +1 -1
- package/esm/types/std/Option.d.ts +1 -2
- package/esm/types/std/Option.d.ts.map +1 -1
- package/esm/types/std/Option.js +1 -2
- package/esm/types/std/Option.js.map +1 -1
- package/esm/types/std/mod.d.ts +0 -1
- package/esm/types/std/mod.d.ts.map +1 -1
- package/esm/types/std/mod.js +0 -1
- package/esm/types/std/mod.js.map +1 -1
- package/esm/types/taggedUnion.d.ts +5 -3
- package/esm/types/taggedUnion.d.ts.map +1 -1
- package/esm/types/taggedUnion.js +16 -19
- package/esm/types/taggedUnion.js.map +1 -1
- package/esm/util/util_types.d.ts +2 -0
- package/esm/util/util_types.d.ts.map +1 -0
- package/esm/util/util_types.js +2 -0
- package/esm/util/util_types.js.map +1 -0
- package/package.json +1 -1
- package/src/types/Ty.ts +8 -7
- package/src/types/constantUnion.ts +1 -0
- package/src/types/primitives.ts +3 -13
- package/src/types/std/Option.ts +2 -3
- package/src/types/std/mod.ts +0 -1
- package/src/types/taggedUnion.ts +20 -30
- package/src/util/util_types.ts +3 -0
- package/esm/types/std/Wrapper.d.ts +0 -6
- package/esm/types/std/Wrapper.d.ts.map +0 -1
- package/esm/types/std/Wrapper.js +0 -5
- package/esm/types/std/Wrapper.js.map +0 -1
- package/src/types/std/Wrapper.ts +0 -6
package/README.md
CHANGED
@@ -6,8 +6,8 @@
|
|
6
6
|
instructions.
|
7
7
|
- [OpenAI Guide →](https://platform.openai.com/docs/guides/structured-outputs)<br />The OpenAI
|
8
8
|
platform's guide on Structured Outputs
|
9
|
-
- [Pattern Libraries →](https://structured-outputs.dev/patterns)<br />Pattern libraries
|
10
|
-
|
9
|
+
- [Pattern Libraries →](https://structured-outputs.dev/patterns)<br />Pattern libraries for
|
10
|
+
common use cases with Structured Outputs TypeScript.
|
11
11
|
|
12
12
|
## Installation
|
13
13
|
|
package/esm/types/Ty.d.ts
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
import type { EnsureLiteralKeys } from "../util/util_types.js";
|
1
2
|
export interface Ty<T = any, P extends keyof any = keyof any, R extends boolean = boolean> {
|
2
|
-
<P2 extends Array<keyof any>>(template: TemplateStringsArray, ...
|
3
|
+
<P2 extends Array<keyof any>>(template: TemplateStringsArray, ...params: EnsureLiteralKeys<P2>): Ty<T, P | P2[number], R>;
|
3
4
|
/** The native TypeScript type. The runtime value is nonexistent. */
|
4
5
|
T: T;
|
5
|
-
/** The literal types of the
|
6
|
+
/** The literal types of the parameter keys. The runtime value is nonexistent. */
|
6
7
|
P: P;
|
7
8
|
"": {
|
8
9
|
subschema: Subschema;
|
@@ -11,7 +12,7 @@ export interface Ty<T = any, P extends keyof any = keyof any, R extends boolean
|
|
11
12
|
context: Array<Context>;
|
12
13
|
applied: Applied;
|
13
14
|
};
|
14
|
-
/**
|
15
|
+
/** Apply context to parameters. */
|
15
16
|
fill: <A extends Partial<Record<P, number | string>>>(values: A) => Ty<T, Exclude<P, keyof A>, R>;
|
16
17
|
/** Get the corresponding JSON Schema. */
|
17
18
|
schema(this: Ty<T, never>): Schema;
|
@@ -22,7 +23,7 @@ export type SubschemaFactory = (ty: Ty) => Schema;
|
|
22
23
|
export declare function SubschemaFactory(applied: Applied): SubschemaFactory;
|
23
24
|
interface Context {
|
24
25
|
template: TemplateStringsArray;
|
25
|
-
|
26
|
+
params: Array<keyof any>;
|
26
27
|
}
|
27
28
|
type Applied = Record<keyof any, number | string>;
|
28
29
|
export type Schema = Record<string, unknown>;
|
package/esm/types/Ty.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Ty.d.ts","sourceRoot":"","sources":["../../src/types/Ty.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"Ty.d.ts","sourceRoot":"","sources":["../../src/types/Ty.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAE9D,MAAM,WAAW,EAAE,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,SAAS,MAAM,GAAG,GAAG,MAAM,GAAG,EAAE,CAAC,SAAS,OAAO,GAAG,OAAO;IACvF,CAAC,EAAE,SAAS,KAAK,CAAC,MAAM,GAAG,CAAC,EAC1B,QAAQ,EAAE,oBAAoB,EAC9B,GAAG,MAAM,EAAE,iBAAiB,CAAC,EAAE,CAAC,GAC/B,EAAE,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAA;IAE3B,oEAAoE;IACpE,CAAC,EAAE,CAAC,CAAA;IACJ,iFAAiF;IACjF,CAAC,EAAE,CAAC,CAAA;IAEJ,EAAE,EAAE;QACF,SAAS,EAAE,SAAS,CAAA;QACpB,IAAI,EAAE,CAAC,CAAA;QACP,SAAS,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,GAAG,CAAA;QAC9B,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;QACvB,OAAO,EAAE,OAAO,CAAA;KACjB,CAAA;IAED,mCAAmC;IACnC,IAAI,EAAE,CAAC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,EAClD,MAAM,EAAE,CAAC,KACN,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAElC,yCAAyC;IACzC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,CAAA;CACnC;AAED,wBAAgB,EAAE,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,GAAG,EAAE,CAAC,SAAS,OAAO,EAAE,CAAC,GAAG,CAAC,EACjE,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,CAAC,EACP,SAAS,GAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAA6B,EACtD,OAAO,GAAE,KAAK,CAAC,OAAO,CAAM,EAC5B,OAAO,GAAE,OAAY,GACpB,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CA8Bb;AAED,MAAM,MAAM,SAAS,GAAG,CAAC,SAAS,EAAE,gBAAgB,KAAK,MAAM,CAAA;AAE/D,MAAM,MAAM,gBAAgB,GAAG,CAAC,EAAE,EAAE,EAAE,KAAK,MAAM,CAAA;AACjD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,gBAAgB,CAWnE;AAED,UAAU,OAAO;IACf,QAAQ,EAAE,oBAAoB,CAAA;IAC9B,MAAM,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAA;CACzB;AACD,KAAK,OAAO,GAAG,MAAM,CAAC,MAAM,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,CAAA;AAEjD,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA"}
|
package/esm/types/Ty.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { recombine } from "../util/recombine.js";
|
2
2
|
export function Ty(subschema, root, transform = (value) => value, context = [], applied = {}) {
|
3
|
-
return Object.assign((template, ...
|
3
|
+
return Object.assign((template, ...params) => Ty(subschema, root, transform, [{ template, params }, ...context], applied), {}, {
|
4
4
|
"": {
|
5
5
|
subschema,
|
6
6
|
root,
|
@@ -23,7 +23,7 @@ export function SubschemaFactory(applied) {
|
|
23
23
|
return (ty) => {
|
24
24
|
applied = { ...applied, ...ty[""].applied };
|
25
25
|
const description = ty[""].context
|
26
|
-
.map(({ template,
|
26
|
+
.map(({ template, params }) => recombine(template, params.map((k) => applied[k])))
|
27
27
|
.join(" ");
|
28
28
|
return {
|
29
29
|
...ty[""].subschema(SubschemaFactory(applied)),
|
package/esm/types/Ty.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Ty.js","sourceRoot":"","sources":["../../src/types/Ty.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;
|
1
|
+
{"version":3,"file":"Ty.js","sourceRoot":"","sources":["../../src/types/Ty.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AA+BhD,MAAM,UAAU,EAAE,CAChB,SAAoB,EACpB,IAAO,EACP,YAA6B,CAAC,KAAK,EAAE,EAAE,CAAC,KAAc,EACtD,UAA0B,EAAE,EAC5B,UAAmB,EAAE;IAErB,OAAO,MAAM,CAAC,MAAM,CAClB,CAA8B,QAA8B,EAAE,GAAG,MAAU,EAAE,EAAE,CAC7E,EAAE,CACA,SAAS,EACT,IAAI,EACJ,SAAS,EACT,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,GAAG,OAAO,CAAC,EAClC,OAAO,CACR,EACH,EAAoB,EACpB;QACE,EAAE,EAAE;YACF,SAAS;YACT,IAAI;YACJ,SAAS;YACT,OAAO;YACP,OAAO;SACR;QACD,IAAI,EAAE,CAAgD,MAAS,EAAE,EAAE;YACjE,OAAO,EAAE,CAA+B,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE;gBAC3E,GAAG,OAAO;gBACV,GAAG,MAAM;aACV,CAAC,CAAA;QACJ,CAAC;QACD,MAAM;YACJ,OAAO,gBAAgB,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAA;QACnC,CAAC;KACF,CACF,CAAA;AACH,CAAC;AAKD,MAAM,UAAU,gBAAgB,CAAC,OAAgB;IAC/C,OAAO,CAAC,EAAE,EAAE,EAAE;QACZ,OAAO,GAAG,EAAE,GAAG,OAAO,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAA;QAC3C,MAAM,WAAW,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,OAAO;aAC/B,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC;aAClF,IAAI,CAAC,GAAG,CAAC,CAAA;QACZ,OAAO;YACL,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;YAC9C,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE;SACtC,CAAA;IACH,CAAC,CAAA;AACH,CAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"constantUnion.d.ts","sourceRoot":"","sources":["../../src/types/constantUnion.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;
|
1
|
+
{"version":3,"file":"constantUnion.d.ts","sourceRoot":"","sources":["../../src/types/constantUnion.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAG5B,wBAAgB,aAAa,CAAC,CAAC,SAAS,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,EAC5D,GAAG,OAAO,EAAE,CAAC,GACZ,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAkB7B"}
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { partition } from "../deps/jsr.io/@std/collections/1.0.9/mod.js";
|
2
2
|
import { Ty } from "./Ty.js";
|
3
|
+
// TODO: use single `enum` type in cases where there aren't both numbers and strings
|
3
4
|
export function constantUnion(...members) {
|
4
5
|
const [strings, numbers] = partition(members, (v) => typeof v === "string");
|
5
6
|
return Ty(() => ({
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"constantUnion.js","sourceRoot":"","sources":["../../src/types/constantUnion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,8CAA8C,CAAA;AACxE,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,MAAM,UAAU,aAAa,CAC3B,GAAG,OAAU;IAEb,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAA;IAC3E,OAAO,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;QACf,KAAK,EAAE;YACL,GAAG,OAAO,CAAC,MAAM;gBACf,CAAC,CAAC,CAAC;wBACD,IAAI,EAAE,QAAQ;wBACd,IAAI,EAAE,OAAO;qBACd,CAAC;gBACF,CAAC,CAAC,EAAE;YACN,GAAG,OAAO,CAAC,MAAM;gBACf,CAAC,CAAC,CAAC;wBACD,IAAI,EAAE,QAAQ;wBACd,IAAI,EAAE,OAAO;qBACd,CAAC;gBACF,CAAC,CAAC,EAAE;SACP;KACF,CAAC,EAAE,KAAK,CAAC,CAAA;AACZ,CAAC"}
|
1
|
+
{"version":3,"file":"constantUnion.js","sourceRoot":"","sources":["../../src/types/constantUnion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,8CAA8C,CAAA;AACxE,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,oFAAoF;AACpF,MAAM,UAAU,aAAa,CAC3B,GAAG,OAAU;IAEb,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAA;IAC3E,OAAO,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;QACf,KAAK,EAAE;YACL,GAAG,OAAO,CAAC,MAAM;gBACf,CAAC,CAAC,CAAC;wBACD,IAAI,EAAE,QAAQ;wBACd,IAAI,EAAE,OAAO;qBACd,CAAC;gBACF,CAAC,CAAC,EAAE;YACN,GAAG,OAAO,CAAC,MAAM;gBACf,CAAC,CAAC,CAAC;wBACD,IAAI,EAAE,QAAQ;wBACd,IAAI,EAAE,OAAO;qBACd,CAAC;gBACF,CAAC,CAAC,EAAE;SACP;KACF,CAAC,EAAE,KAAK,CAAC,CAAA;AACZ,CAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"primitives.d.ts","sourceRoot":"","sources":["../../src/types/primitives.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,eAAO,MAAM,
|
1
|
+
{"version":3,"file":"primitives.d.ts","sourceRoot":"","sources":["../../src/types/primitives.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,eAAO,MAAM,OAAO,EAAE,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,KAAK,CAA0C,CAAA;AAExF,eAAO,MAAM,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,CAAyC,CAAA;AAErF,eAAO,MAAM,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,CAAyC,CAAA"}
|
package/esm/types/primitives.js
CHANGED
@@ -1,14 +1,5 @@
|
|
1
1
|
import { Ty } from "./Ty.js";
|
2
|
-
export const
|
3
|
-
|
4
|
-
}), false);
|
5
|
-
export const boolean = Ty(() => ({
|
6
|
-
type: "boolean",
|
7
|
-
}), false);
|
8
|
-
export const number = Ty(() => ({
|
9
|
-
type: "number",
|
10
|
-
}), false);
|
11
|
-
export const string = Ty(() => ({
|
12
|
-
type: "string",
|
13
|
-
}), false);
|
2
|
+
export const boolean = Ty(() => ({ type: "boolean" }), false);
|
3
|
+
export const number = Ty(() => ({ type: "number" }), false);
|
4
|
+
export const string = Ty(() => ({ type: "string" }), false);
|
14
5
|
//# sourceMappingURL=primitives.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"primitives.js","sourceRoot":"","sources":["../../src/types/primitives.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,MAAM,CAAC,MAAM,
|
1
|
+
{"version":3,"file":"primitives.js","sourceRoot":"","sources":["../../src/types/primitives.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,MAAM,CAAC,MAAM,OAAO,GAA8B,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,EAAE,KAAK,CAAC,CAAA;AAExF,MAAM,CAAC,MAAM,MAAM,GAA6B,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,EAAE,KAAK,CAAC,CAAA;AAErF,MAAM,CAAC,MAAM,MAAM,GAA6B,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,EAAE,KAAK,CAAC,CAAA"}
|
@@ -1,8 +1,7 @@
|
|
1
|
-
import { none } from "../primitives.js";
|
2
1
|
import { taggedUnion } from "../taggedUnion.js";
|
3
2
|
import type { Ty } from "../Ty.js";
|
4
3
|
export declare function Option<S extends Ty>(Some: S): ReturnType<typeof taggedUnion<{
|
5
4
|
Some: S;
|
6
|
-
None:
|
5
|
+
None: null;
|
7
6
|
}>>;
|
8
7
|
//# sourceMappingURL=Option.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Option.d.ts","sourceRoot":"","sources":["../../../src/types/std/Option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
1
|
+
{"version":3,"file":"Option.d.ts","sourceRoot":"","sources":["../../../src/types/std/Option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,UAAU,CAAA;AAElC,wBAAgB,MAAM,CAAC,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,CAAC,GAAG,UAAU,CACvD,OAAO,WAAW,CAAC;IACjB,IAAI,EAAE,CAAC,CAAA;IACP,IAAI,EAAE,IAAI,CAAA;CACX,CAAC,CACH,CAKA"}
|
package/esm/types/std/Option.js
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
import { none } from "../primitives.js";
|
2
1
|
import { taggedUnion } from "../taggedUnion.js";
|
3
2
|
export function Option(Some) {
|
4
3
|
return taggedUnion({
|
5
4
|
Some,
|
6
|
-
None:
|
5
|
+
None: null,
|
7
6
|
}) `An option of specified \`some\` type.`;
|
8
7
|
}
|
9
8
|
//# sourceMappingURL=Option.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Option.js","sourceRoot":"","sources":["../../../src/types/std/Option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
1
|
+
{"version":3,"file":"Option.js","sourceRoot":"","sources":["../../../src/types/std/Option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAG/C,MAAM,UAAU,MAAM,CAAe,IAAO;IAM1C,OAAO,WAAW,CAAC;QACjB,IAAI;QACJ,IAAI,EAAE,IAAI;KACX,CAAC,CAAA,uCAAuC,CAAA;AAC3C,CAAC"}
|
package/esm/types/std/mod.d.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../src/types/std/mod.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../../src/types/std/mod.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA"}
|
package/esm/types/std/mod.js
CHANGED
package/esm/types/std/mod.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../../src/types/std/mod.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../../src/types/std/mod.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA"}
|
@@ -1,8 +1,10 @@
|
|
1
1
|
import { Ty } from "./Ty.js";
|
2
|
-
export declare function taggedUnion<M extends Record<string, Ty>>(members: M): Ty<{
|
3
|
-
[K in keyof M]: {
|
2
|
+
export declare function taggedUnion<M extends Record<string, Ty | null>>(members: M): Ty<{
|
3
|
+
[K in keyof M]: M[K] extends Ty ? {
|
4
4
|
type: K;
|
5
5
|
value: M[K]["T"];
|
6
|
+
} : {
|
7
|
+
type: K;
|
6
8
|
};
|
7
|
-
}[keyof M], M[keyof M]["P"], false>;
|
9
|
+
}[keyof M], Exclude<M[keyof M], null>["P"], false>;
|
8
10
|
//# sourceMappingURL=taggedUnion.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"taggedUnion.d.ts","sourceRoot":"","sources":["../../src/types/taggedUnion.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"taggedUnion.d.ts","sourceRoot":"","sources":["../../src/types/taggedUnion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,EAAE,GAAG,IAAI,CAAC,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,CAC9E;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,GAAG;QAAE,IAAI,EAAE,CAAC,CAAC;QAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;KAAE,GAAG;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE;CAAE,CAAC,MAAM,CAAC,CAAC,EAC1F,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,EAC9B,KAAK,CACN,CAuBA"}
|
package/esm/types/taggedUnion.js
CHANGED
@@ -1,25 +1,22 @@
|
|
1
1
|
import { Ty } from "./Ty.js";
|
2
2
|
export function taggedUnion(members) {
|
3
3
|
const entries = Object.entries(members);
|
4
|
-
return Ty((subschema) =>
|
5
|
-
|
6
|
-
: {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
type: "string",
|
17
|
-
const: type,
|
4
|
+
return Ty((subschema) => ({
|
5
|
+
discriminator: "type",
|
6
|
+
anyOf: entries.map(([k, v]) => ({
|
7
|
+
type: "object",
|
8
|
+
properties: {
|
9
|
+
type: {
|
10
|
+
type: "string",
|
11
|
+
const: k,
|
12
|
+
},
|
13
|
+
...(v === null ? {} : {
|
14
|
+
value: subschema(v),
|
15
|
+
}),
|
18
16
|
},
|
19
|
-
value,
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
};
|
17
|
+
required: ["type", ...v === null ? [] : ["value"]],
|
18
|
+
additionalProperties: false,
|
19
|
+
})),
|
20
|
+
}), false, (v) => members[v.type][""].transform(v));
|
24
21
|
}
|
25
22
|
//# sourceMappingURL=taggedUnion.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"taggedUnion.js","sourceRoot":"","sources":["../../src/types/taggedUnion.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"taggedUnion.js","sourceRoot":"","sources":["../../src/types/taggedUnion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,MAAM,UAAU,WAAW,CAAsC,OAAU;IAKzE,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IACvC,OAAO,EAAE,CACP,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;QACd,aAAa,EAAE,MAAM;QACrB,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAC9B,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,IAAI,EAAE;oBACJ,IAAI,EAAE,QAAQ;oBACd,KAAK,EAAE,CAAC;iBACT;gBACD,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;oBACpB,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC;iBACpB,CAAC;aACH;YACD,QAAQ,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;YAClD,oBAAoB,EAAE,KAAK;SAC5B,CAAC,CAAC;KACJ,CAAC,EACF,KAAK,EACL,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAE,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CACzC,CAAA;AACH,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"util_types.d.ts","sourceRoot":"","sources":["../../src/util/util_types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,iBAAiB,CAAC,CAAC,SAAS,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,SACvF,KAAK,GAAG,CAAC,GACP,KAAK,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"util_types.js","sourceRoot":"","sources":["../../src/util/util_types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
package/src/types/Ty.ts
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
import { recombine } from "../util/recombine.js"
|
2
|
+
import type { EnsureLiteralKeys } from "../util/util_types.js"
|
2
3
|
|
3
4
|
export interface Ty<T = any, P extends keyof any = keyof any, R extends boolean = boolean> {
|
4
5
|
<P2 extends Array<keyof any>>(
|
5
6
|
template: TemplateStringsArray,
|
6
|
-
...
|
7
|
+
...params: EnsureLiteralKeys<P2>
|
7
8
|
): Ty<T, P | P2[number], R>
|
8
9
|
|
9
10
|
/** The native TypeScript type. The runtime value is nonexistent. */
|
10
11
|
T: T
|
11
|
-
/** The literal types of the
|
12
|
+
/** The literal types of the parameter keys. The runtime value is nonexistent. */
|
12
13
|
P: P
|
13
14
|
|
14
15
|
"": {
|
@@ -19,7 +20,7 @@ export interface Ty<T = any, P extends keyof any = keyof any, R extends boolean
|
|
19
20
|
applied: Applied
|
20
21
|
}
|
21
22
|
|
22
|
-
/**
|
23
|
+
/** Apply context to parameters. */
|
23
24
|
fill: <A extends Partial<Record<P, number | string>>>(
|
24
25
|
values: A,
|
25
26
|
) => Ty<T, Exclude<P, keyof A>, R>
|
@@ -36,12 +37,12 @@ export function Ty<T, P extends keyof any, R extends boolean, I = T>(
|
|
36
37
|
applied: Applied = {},
|
37
38
|
): Ty<T, P, R> {
|
38
39
|
return Object.assign(
|
39
|
-
<P2 extends Array<keyof any>>(template: TemplateStringsArray, ...
|
40
|
+
<P2 extends Array<keyof any>>(template: TemplateStringsArray, ...params: P2) =>
|
40
41
|
Ty<T, P | P2[number], R, I>(
|
41
42
|
subschema,
|
42
43
|
root,
|
43
44
|
transform,
|
44
|
-
[{ template,
|
45
|
+
[{ template, params }, ...context],
|
45
46
|
applied,
|
46
47
|
),
|
47
48
|
{} as { T: T; P: P },
|
@@ -73,7 +74,7 @@ export function SubschemaFactory(applied: Applied): SubschemaFactory {
|
|
73
74
|
return (ty) => {
|
74
75
|
applied = { ...applied, ...ty[""].applied }
|
75
76
|
const description = ty[""].context
|
76
|
-
.map(({ template,
|
77
|
+
.map(({ template, params }) => recombine(template, params.map((k) => applied[k]!)))
|
77
78
|
.join(" ")
|
78
79
|
return {
|
79
80
|
...ty[""].subschema(SubschemaFactory(applied)),
|
@@ -84,7 +85,7 @@ export function SubschemaFactory(applied: Applied): SubschemaFactory {
|
|
84
85
|
|
85
86
|
interface Context {
|
86
87
|
template: TemplateStringsArray
|
87
|
-
|
88
|
+
params: Array<keyof any>
|
88
89
|
}
|
89
90
|
type Applied = Record<keyof any, number | string>
|
90
91
|
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { partition } from "../deps/jsr.io/@std/collections/1.0.9/mod.js"
|
2
2
|
import { Ty } from "./Ty.js"
|
3
3
|
|
4
|
+
// TODO: use single `enum` type in cases where there aren't both numbers and strings
|
4
5
|
export function constantUnion<M extends Array<number | string>>(
|
5
6
|
...members: M
|
6
7
|
): Ty<M[number], never, false> {
|
package/src/types/primitives.ts
CHANGED
@@ -1,17 +1,7 @@
|
|
1
1
|
import { Ty } from "./Ty.js"
|
2
2
|
|
3
|
-
export const
|
4
|
-
type: "null",
|
5
|
-
}), false)
|
3
|
+
export const boolean: Ty<boolean, never, false> = Ty(() => ({ type: "boolean" }), false)
|
6
4
|
|
7
|
-
export const
|
8
|
-
type: "boolean",
|
9
|
-
}), false)
|
5
|
+
export const number: Ty<number, never, false> = Ty(() => ({ type: "number" }), false)
|
10
6
|
|
11
|
-
export const
|
12
|
-
type: "number",
|
13
|
-
}), false)
|
14
|
-
|
15
|
-
export const string: Ty<string, never, false> = Ty(() => ({
|
16
|
-
type: "string",
|
17
|
-
}), false)
|
7
|
+
export const string: Ty<string, never, false> = Ty(() => ({ type: "string" }), false)
|
package/src/types/std/Option.ts
CHANGED
@@ -1,15 +1,14 @@
|
|
1
|
-
import { none } from "../primitives.js"
|
2
1
|
import { taggedUnion } from "../taggedUnion.js"
|
3
2
|
import type { Ty } from "../Ty.js"
|
4
3
|
|
5
4
|
export function Option<S extends Ty>(Some: S): ReturnType<
|
6
5
|
typeof taggedUnion<{
|
7
6
|
Some: S
|
8
|
-
None:
|
7
|
+
None: null
|
9
8
|
}>
|
10
9
|
> {
|
11
10
|
return taggedUnion({
|
12
11
|
Some,
|
13
|
-
None:
|
12
|
+
None: null,
|
14
13
|
})`An option of specified \`some\` type.`
|
15
14
|
}
|
package/src/types/std/mod.ts
CHANGED
package/src/types/taggedUnion.ts
CHANGED
@@ -1,40 +1,30 @@
|
|
1
|
-
import {
|
1
|
+
import { Ty } from "./Ty.js"
|
2
2
|
|
3
|
-
export function taggedUnion<M extends Record<string, Ty>>(members: M): Ty<
|
4
|
-
{
|
5
|
-
|
6
|
-
type: K
|
7
|
-
value: M[K]["T"]
|
8
|
-
}
|
9
|
-
}[keyof M],
|
10
|
-
M[keyof M]["P"],
|
3
|
+
export function taggedUnion<M extends Record<string, Ty | null>>(members: M): Ty<
|
4
|
+
{ [K in keyof M]: M[K] extends Ty ? { type: K; value: M[K]["T"] } : { type: K } }[keyof M],
|
5
|
+
Exclude<M[keyof M], null>["P"],
|
11
6
|
false
|
12
7
|
> {
|
13
8
|
const entries = Object.entries(members)
|
14
9
|
return Ty(
|
15
|
-
(subschema) =>
|
16
|
-
|
17
|
-
|
18
|
-
:
|
19
|
-
|
20
|
-
|
10
|
+
(subschema) => ({
|
11
|
+
discriminator: "type",
|
12
|
+
anyOf: entries.map(([k, v]) => ({
|
13
|
+
type: "object",
|
14
|
+
properties: {
|
15
|
+
type: {
|
16
|
+
type: "string",
|
17
|
+
const: k,
|
18
|
+
},
|
19
|
+
...(v === null ? {} : {
|
20
|
+
value: subschema(v),
|
21
|
+
}),
|
21
22
|
},
|
23
|
+
required: ["type", ...v === null ? [] : ["value"]],
|
24
|
+
additionalProperties: false,
|
25
|
+
})),
|
26
|
+
}),
|
22
27
|
false,
|
23
28
|
(v) => members[v.type]![""].transform(v),
|
24
29
|
)
|
25
30
|
}
|
26
|
-
|
27
|
-
function variant(type: string, value: Schema) {
|
28
|
-
return {
|
29
|
-
type: "object",
|
30
|
-
properties: {
|
31
|
-
type: {
|
32
|
-
type: "string",
|
33
|
-
const: type,
|
34
|
-
},
|
35
|
-
value,
|
36
|
-
},
|
37
|
-
required: ["type", "value"],
|
38
|
-
additionalProperties: false,
|
39
|
-
}
|
40
|
-
}
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"Wrapper.d.ts","sourceRoot":"","sources":["../../../src/types/std/Wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,wBAAgB,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE,KAAK,EAAE,CAAC,GAAG,UAAU,CAAC,OAAO,MAAM,CAAC;IAAE,KAAK,EAAE,CAAC,CAAA;CAAE,CAAC,CAAC,CAEvF"}
|
package/esm/types/std/Wrapper.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"Wrapper.js","sourceRoot":"","sources":["../../../src/types/std/Wrapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,MAAM,UAAU,OAAO,CAAe,KAAQ;IAC5C,OAAO,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAA;AAC1B,CAAC"}
|