Skip to content

Commit 3a737e2

Browse files
author
Daniele Fedeli
authored
Infer type schema (#514)
* Infer type schema (#4) * Added type inference * Test cases with tsd * Allow manual inference * Restored index.js * Restore unwanted files * Removed json-schema-to-ts
1 parent 34844c6 commit 3a737e2

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

index.d.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Ajv, { Options as AjvOptions } from "ajv"
2+
23
declare namespace build {
34
interface BaseSchema {
45
/**
@@ -189,13 +190,13 @@ interface StandaloneOption extends build.Options {
189190
declare function build(schema: build.AnySchema, options: DebugOption): { code: string, ajv: Ajv };
190191
declare function build(schema: build.AnySchema, options: DeprecateDebugOption): { code: string, ajv: Ajv };
191192
declare function build(schema: build.AnySchema, options: StandaloneOption): string;
192-
declare function build(schema: build.AnySchema, options?: build.Options): (doc: any) => any;
193-
declare function build(schema: build.StringSchema, options?: build.Options): (doc: string) => string;
194-
declare function build(schema: build.IntegerSchema | build.NumberSchema, options?: build.Options): (doc: number) => string;
195-
declare function build(schema: build.NullSchema, options?: build.Options): (doc: null) => "null";
196-
declare function build(schema: build.BooleanSchema, options?: build.Options): (doc: boolean) => string;
197-
declare function build(schema: build.ArraySchema | build.TupleSchema, options?: build.Options): (doc: any[]) => string;
198-
declare function build(schema: build.ObjectSchema, options?: build.Options): (doc: object) => string;
199-
declare function build(schema: build.Schema, options?: build.Options): (doc: object | any[] | string | number | boolean | null) => string;
193+
declare function build(schema: build.AnySchema, options?: build.Options): <TDoc = any>(doc: TDoc) => any;
194+
declare function build(schema: build.StringSchema, options?: build.Options): <TDoc extends string = string>(doc: TDoc) => string;
195+
declare function build(schema: build.IntegerSchema | build.NumberSchema, options?: build.Options): <TDoc extends number = number>(doc: TDoc) => string;
196+
declare function build(schema: build.NullSchema, options?: build.Options): <TDoc extends null = null>(doc: TDoc) => "null";
197+
declare function build(schema: build.BooleanSchema, options?: build.Options): <TDoc extends boolean = boolean>(doc: TDoc) => string;
198+
declare function build(schema: build.ArraySchema | build.TupleSchema, options?: build.Options): <TDoc extends any[]= any[]>(doc: TDoc) => string;
199+
declare function build(schema: build.ObjectSchema, options?: build.Options): <TDoc extends object = object>(doc: TDoc) => string;
200+
declare function build(schema: build.Schema, options?: build.Options): <TDoc = object | any[] | string | number | boolean | null> (doc: TDoc) => string;
200201

201202
export = build;

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"benchmark": "node ./benchmark/bench-cmp-lib.js",
1212
"lint:fix": "standard --fix",
1313
"test:lint": "standard",
14-
"test:typescript": "tsc --project ./test/types/tsconfig.json",
14+
"test:typescript": "tsc --project ./test/types/tsconfig.json && tsd",
1515
"test:unit": "tap -J test/*.test.js test/**/*.test.js",
1616
"test": "npm run test:lint && npm run test:unit && npm run test:typescript"
1717
},
@@ -45,6 +45,7 @@
4545
"simple-git": "^3.7.1",
4646
"standard": "^17.0.0",
4747
"tap": "^16.0.1",
48+
"tsd": "^0.22.0",
4849
"typescript": "^4.0.2",
4950
"webpack": "^5.40.0"
5051
},
@@ -61,5 +62,8 @@
6162
"schema-validator.js"
6263
]
6364
},
64-
"runkitExampleFilename": "./examples/example.js"
65+
"runkitExampleFilename": "./examples/example.js",
66+
"tsd": {
67+
"directory": "test/types"
68+
}
6569
}

test/types/schema-inference.test-d.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { expectError } from "tsd";
2+
import build from "../..";
3+
4+
// With inference
5+
interface Schema {
6+
id: string;
7+
a?: number;
8+
}
9+
10+
const stringify3 = build({
11+
type: "object",
12+
properties: { a: { type: "string" } },
13+
});
14+
stringify3<Schema>({ id: "123" });
15+
stringify3<Schema>({ a: 123, id: "123" });
16+
expectError(stringify3<Schema>({ anotherOne: "bar" }));
17+
expectError(stringify3<Schema>({ a: "bar" }));
18+
19+
// Without inference
20+
const stringify4 = build({
21+
type: "object",
22+
properties: { a: { type: "string" } },
23+
});
24+
stringify4({ id: "123" });
25+
stringify4({ a: 123, id: "123" });
26+
stringify4({ anotherOne: "bar" });
27+
stringify4({ a: "bar" });
28+
29+
// Without inference - string type
30+
const stringify5 = build({
31+
type: "string",
32+
});
33+
stringify5("foo");
34+
expectError(stringify5({ id: "123" }));
35+
36+
// Without inference - null type
37+
const stringify6 = build({
38+
type: "null",
39+
});
40+
stringify6(null);
41+
expectError(stringify6("a string"));
42+
43+
// Without inference - boolean type
44+
const stringify7 = build({
45+
type: "boolean",
46+
});
47+
stringify7(true);
48+
expectError(stringify7("a string"));

0 commit comments

Comments
 (0)