Skip to content

Commit c8f4d5c

Browse files
committed
fix: 🐛 correct various TypeScript converter issues
1 parent 828ae32 commit c8f4d5c

File tree

5 files changed

+40
-95
lines changed

5 files changed

+40
-95
lines changed

src/json-schema/converter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ function mapToJsonSchema(type: MapType<any>, ctx?: TypeExportContext): JsonSchem
215215
const result: JsonSchemaObject = {
216216
type: 'object',
217217
patternProperties: {
218-
'.*': typeToJsonSchema((type as any).valueType, ctx),
218+
'.*': typeToJsonSchema(type._value, ctx),
219219
},
220220
};
221221

src/type/__tests__/getJsonSchema.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {t} from '..';
2+
import {typeToJsonSchema} from '../../json-schema';
23
import {TypeSystem} from '../../system';
34

45
test('can print a type', () => {
56
const type = t
67
.Object(
7-
t.prop('id', t.str.options({validator: ['id', 'uuid']})).options({
8+
t.prop('id', t.str).options({
89
description: 'The id of the object',
910
}),
1011
t.prop('tags', t.Array(t.str).options({title: 'Tags'})).options({title: 'Always use tags'}),
@@ -31,7 +32,7 @@ test('can print a type', () => {
3132
t
3233
.Binary(
3334
t
34-
.Tuple(t.Const(7 as const).options({description: '7 is the magic number'}), t.str, t.any)
35+
.tuple(t.Const(7 as const).options({description: '7 is the magic number'}), t.str, t.any)
3536
.options({description: 'Should always have 3 elements'}),
3637
)
3738
.options({format: 'cbor'}),
@@ -40,7 +41,7 @@ test('can print a type', () => {
4041
)
4142
.options({unknownFields: true});
4243
// console.log(JSON.stringify(type.toJsonSchema(), null, 2));
43-
expect(type.toJsonSchema()).toMatchInlineSnapshot(`
44+
expect(typeToJsonSchema(type)).toMatchInlineSnapshot(`
4445
{
4546
"properties": {
4647
"arrayProperty": {
@@ -199,6 +200,6 @@ test('exports "ref" type to JSON Schema "$defs"', () => {
199200
const system = new TypeSystem();
200201
const t = system.t;
201202
const type = t.Object(t.prop('id', t.str), t.prop('user', t.Ref('User')));
202-
const schema = type.toJsonSchema() as any;
203+
const schema = typeToJsonSchema(type) as any;
203204
expect(schema.properties.user.$ref).toBe('#/$defs/User');
204205
});

src/type/__tests__/toJson.spec.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/type/__tests__/toTypeScriptAst.spec.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {TypeSystem} from '../../system';
2+
import {toTypeScriptAst} from '../../typescript/converter';
23

34
describe('any', () => {
45
test('can encode "any" type', () => {
56
const system = new TypeSystem();
67
const type = system.t.any;
7-
expect(type.toTypeScriptAst()).toEqual({
8+
expect(toTypeScriptAst(type)).toEqual({
89
node: 'AnyKeyword',
910
});
1011
});
@@ -14,7 +15,7 @@ describe('const', () => {
1415
test('can handle number const', () => {
1516
const system = new TypeSystem();
1617
const type = system.t.Const<123>(123);
17-
expect(type.toTypeScriptAst()).toEqual({
18+
expect(toTypeScriptAst(type)).toEqual({
1819
node: 'NumericLiteral',
1920
text: '123',
2021
});
@@ -23,31 +24,31 @@ describe('const', () => {
2324
test('can handle null', () => {
2425
const system = new TypeSystem();
2526
const type = system.t.Const<null>(null);
26-
expect(type.toTypeScriptAst()).toEqual({
27+
expect(toTypeScriptAst(type)).toEqual({
2728
node: 'NullKeyword',
2829
});
2930
});
3031

3132
test('can handle "true"', () => {
3233
const system = new TypeSystem();
3334
const type = system.t.Const<true>(true);
34-
expect(type.toTypeScriptAst()).toEqual({
35+
expect(toTypeScriptAst(type)).toEqual({
3536
node: 'TrueKeyword',
3637
});
3738
});
3839

3940
test('can handle "false"', () => {
4041
const system = new TypeSystem();
4142
const type = system.t.Const<false>(false);
42-
expect(type.toTypeScriptAst()).toEqual({
43+
expect(toTypeScriptAst(type)).toEqual({
4344
node: 'FalseKeyword',
4445
});
4546
});
4647

4748
test('can handle string', () => {
4849
const system = new TypeSystem();
4950
const type = system.t.Const<'asdf'>('asdf');
50-
expect(type.toTypeScriptAst()).toEqual({
51+
expect(toTypeScriptAst(type)).toEqual({
5152
node: 'StringLiteral',
5253
text: 'asdf',
5354
});
@@ -56,7 +57,7 @@ describe('const', () => {
5657
test('complex objects', () => {
5758
const system = new TypeSystem();
5859
const type = system.t.Const({foo: 'bar'} as const);
59-
expect(type.toTypeScriptAst()).toEqual({
60+
expect(toTypeScriptAst(type)).toEqual({
6061
node: 'ObjectKeyword',
6162
});
6263
});
@@ -66,7 +67,7 @@ describe('bool', () => {
6667
test('can emit boolean AST', () => {
6768
const system = new TypeSystem();
6869
const type = system.t.bool;
69-
expect(type.toTypeScriptAst()).toEqual({
70+
expect(toTypeScriptAst(type)).toEqual({
7071
node: 'BooleanKeyword',
7172
});
7273
});
@@ -76,7 +77,7 @@ describe('num', () => {
7677
test('can emit number AST', () => {
7778
const system = new TypeSystem();
7879
const type = system.t.num;
79-
expect(type.toTypeScriptAst()).toEqual({
80+
expect(toTypeScriptAst(type)).toEqual({
8081
node: 'NumberKeyword',
8182
});
8283
});
@@ -86,7 +87,7 @@ describe('str', () => {
8687
test('can emit string AST', () => {
8788
const system = new TypeSystem();
8889
const type = system.t.str;
89-
expect(type.toTypeScriptAst()).toEqual({
90+
expect(toTypeScriptAst(type)).toEqual({
9091
node: 'StringKeyword',
9192
});
9293
});
@@ -96,7 +97,7 @@ describe('bin', () => {
9697
test('can emit binary AST', () => {
9798
const system = new TypeSystem();
9899
const type = system.t.bin;
99-
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
100+
expect(toTypeScriptAst(type)).toMatchInlineSnapshot(`
100101
{
101102
"id": {
102103
"name": "Uint8Array",
@@ -112,7 +113,7 @@ describe('arr', () => {
112113
test('can emit array of "any" AST', () => {
113114
const system = new TypeSystem();
114115
const type = system.t.arr;
115-
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
116+
expect(toTypeScriptAst(type)).toMatchInlineSnapshot(`
116117
{
117118
"elementType": {
118119
"node": "AnyKeyword",
@@ -125,7 +126,7 @@ describe('arr', () => {
125126
test('can emit array of "string" AST', () => {
126127
const system = new TypeSystem();
127128
const type = system.t.Array(system.t.str);
128-
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
129+
expect(toTypeScriptAst(type)).toMatchInlineSnapshot(`
129130
{
130131
"elementType": {
131132
"node": "StringKeyword",
@@ -140,8 +141,8 @@ describe('tup', () => {
140141
test('can emit tuple AST', () => {
141142
const system = new TypeSystem();
142143
const {t} = system;
143-
const type = system.t.Tuple(t.str, t.num, t.bool);
144-
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
144+
const type = system.t.tuple(t.str, t.num, t.bool);
145+
expect(toTypeScriptAst(type)).toMatchInlineSnapshot(`
145146
{
146147
"elements": [
147148
{
@@ -154,7 +155,7 @@ describe('tup', () => {
154155
"node": "BooleanKeyword",
155156
},
156157
],
157-
"node": "TupType",
158+
"node": "TupleType",
158159
}
159160
`);
160161
});
@@ -176,7 +177,7 @@ describe('obj', () => {
176177
title: 'title',
177178
description: 'description',
178179
});
179-
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
180+
expect(toTypeScriptAst(type)).toMatchInlineSnapshot(`
180181
{
181182
"comment": "# title
182183
@@ -215,7 +216,7 @@ describe('map', () => {
215216
title: 'title',
216217
description: 'description',
217218
});
218-
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
219+
expect(toTypeScriptAst(type)).toMatchInlineSnapshot(`
219220
{
220221
"node": "TypeReference",
221222
"typeArguments": [
@@ -237,7 +238,7 @@ describe('ref', () => {
237238
const system = new TypeSystem();
238239
const {t} = system;
239240
const type = system.t.Ref('Foo');
240-
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
241+
expect(toTypeScriptAst(type)).toMatchInlineSnapshot(`
241242
{
242243
"id": {
243244
"name": "Foo",
@@ -254,7 +255,7 @@ describe('or', () => {
254255
const system = new TypeSystem();
255256
const {t} = system;
256257
const type = system.t.Or(t.str, t.num);
257-
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
258+
expect(toTypeScriptAst(type)).toMatchInlineSnapshot(`
258259
{
259260
"node": "UnionType",
260261
"types": [
@@ -275,7 +276,7 @@ describe('fn', () => {
275276
const system = new TypeSystem();
276277
const {t} = system;
277278
const type = system.t.Function(t.str, t.num);
278-
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
279+
expect(toTypeScriptAst(type)).toMatchInlineSnapshot(`
279280
{
280281
"node": "FnType",
281282
"parameters": [
@@ -312,7 +313,7 @@ describe('fn$', () => {
312313
const system = new TypeSystem();
313314
const {t} = system;
314315
const type = system.t.Function$(t.str, t.num);
315-
expect(type.toTypeScriptAst()).toMatchInlineSnapshot(`
316+
expect(toTypeScriptAst(type)).toMatchInlineSnapshot(`
316317
{
317318
"node": "FnType",
318319
"parameters": [

src/typescript/converter.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ArrType, ObjType} from '../type/classes';
1+
import {ArrType, FnRxType, FnType, MapType, ObjType, OrType} from '../type/classes';
22
import {Type} from '../type/types';
33
import {TypeAlias} from '../system';
44
import type * as ts from './types';
@@ -171,19 +171,19 @@ export function toTypeScriptAst(type: Type): ts.TsType {
171171
return node;
172172
}
173173
case 'map': {
174-
const mapSchema = type.getSchema();
174+
const map = type as MapType;
175175
const node: ts.TsTypeReference = {
176176
node: 'TypeReference',
177177
typeName: 'Record',
178-
typeArguments: [{node: 'StringKeyword'}, toTypeScriptAst(mapSchema.value)],
178+
typeArguments: [{node: 'StringKeyword'}, toTypeScriptAst(map._value)],
179179
};
180180
return node;
181181
}
182182
case 'or': {
183-
const orSchema = type.getSchema();
183+
const or = type as OrType;
184184
const node: ts.TsUnionType = {
185185
node: 'UnionType',
186-
types: orSchema.types.map((type: any) => toTypeScriptAst(type)),
186+
types: or.types.map((type: any) => toTypeScriptAst(type)),
187187
};
188188
return node;
189189
}
@@ -199,11 +199,7 @@ export function toTypeScriptAst(type: Type): ts.TsType {
199199
return node;
200200
}
201201
case 'fn': {
202-
const fnSchema = type.getSchema();
203-
// Extract schemas from the type instances
204-
const reqSchema = (fnSchema.req as any).getSchema ? (fnSchema.req as any).getSchema() : fnSchema.req;
205-
const resSchema = (fnSchema.res as any).getSchema ? (fnSchema.res as any).getSchema() : fnSchema.res;
206-
202+
const fn = type as FnType<any, any>;
207203
const node: ts.TsFnType = {
208204
node: 'FnType',
209205
parameters: [
@@ -213,7 +209,7 @@ export function toTypeScriptAst(type: Type): ts.TsType {
213209
node: 'Identifier',
214210
name: 'request',
215211
},
216-
type: toTypeScriptAst(reqSchema),
212+
type: toTypeScriptAst(fn.req),
217213
},
218214
],
219215
type: {
@@ -222,17 +218,13 @@ export function toTypeScriptAst(type: Type): ts.TsType {
222218
node: 'Identifier',
223219
name: 'Promise',
224220
},
225-
typeArguments: [toTypeScriptAst(resSchema)],
221+
typeArguments: [toTypeScriptAst(fn.res)],
226222
},
227223
};
228224
return node;
229225
}
230226
case 'fn$': {
231-
const fnSchema = type.getSchema();
232-
// Extract schemas from the type instances
233-
const reqSchema = (fnSchema.req as any).getSchema ? (fnSchema.req as any).getSchema() : fnSchema.req;
234-
const resSchema = (fnSchema.res as any).getSchema ? (fnSchema.res as any).getSchema() : fnSchema.res;
235-
227+
const fn = type as FnRxType<any, any>;
236228
const node: ts.TsFnType = {
237229
node: 'FnType',
238230
parameters: [
@@ -248,7 +240,7 @@ export function toTypeScriptAst(type: Type): ts.TsType {
248240
node: 'Identifier',
249241
name: 'Observable',
250242
},
251-
typeArguments: [toTypeScriptAst(reqSchema)],
243+
typeArguments: [toTypeScriptAst(fn.req)],
252244
},
253245
},
254246
],
@@ -258,7 +250,7 @@ export function toTypeScriptAst(type: Type): ts.TsType {
258250
node: 'Identifier',
259251
name: 'Observable',
260252
},
261-
typeArguments: [toTypeScriptAst(resSchema)],
253+
typeArguments: [toTypeScriptAst(fn.res)],
262254
},
263255
};
264256
return node;

0 commit comments

Comments
 (0)