Skip to content

Commit 6408fa6

Browse files
If no type is specified, type as unknown (#1049)
* Add logic for unknown type * Remove unknown from ts intersections * Make tsIntersectionOf a bit more robust for edge cases * Add basic tests for tsIntersectionOf * update:examples * not intended to be committed
1 parent 04c8379 commit 6408fa6

9 files changed

+137
-103
lines changed

examples/digital-ocean-api.ts

+60-60
Large diffs are not rendered by default.

examples/github-api-next.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -11899,8 +11899,8 @@ export interface components {
1189911899
comments_url: string;
1190011900
owner?: components["schemas"]["simple-user"];
1190111901
truncated?: boolean;
11902-
forks?: (Record<string, never>)[];
11903-
history?: (Record<string, never>)[];
11902+
forks?: (unknown)[];
11903+
history?: (unknown)[];
1190411904
};
1190511905
/**
1190611906
* Public User
@@ -12039,8 +12039,8 @@ export interface components {
1203912039
comments_url: string;
1204012040
owner?: null | components["schemas"]["simple-user"];
1204112041
truncated?: boolean;
12042-
forks?: (Record<string, never>)[];
12043-
history?: (Record<string, never>)[];
12042+
forks?: (unknown)[];
12043+
history?: (unknown)[];
1204412044
}, null]>;
1204512045
url?: string;
1204612046
forks_url?: string;
@@ -19026,8 +19026,8 @@ export interface components {
1902619026
primary_key_id?: number;
1902719027
key_id?: string;
1902819028
public_key?: string;
19029-
emails?: (Record<string, never>)[];
19030-
subkeys?: (Record<string, never>)[];
19029+
emails?: (unknown)[];
19030+
subkeys?: (unknown)[];
1903119031
can_sign?: boolean;
1903219032
can_encrypt_comms?: boolean;
1903319033
can_encrypt_storage?: boolean;
@@ -36760,7 +36760,7 @@ export interface components {
3676036760
}, null]>;
3676136761
created_at?: string;
3676236762
description: string;
36763-
docker_metadata?: (Record<string, never>)[];
36763+
docker_metadata?: (unknown)[];
3676436764
draft?: boolean;
3676536765
/** Format: uri */
3676636766
html_url: string;
@@ -36887,7 +36887,7 @@ export interface components {
3688736887
/** Format: uri */
3688836888
url: string;
3688936889
};
36890-
rubygems_metadata?: (Record<string, never>)[];
36890+
rubygems_metadata?: (unknown)[];
3689136891
source_url?: string;
3689236892
summary: string;
3689336893
tag_name?: string;
@@ -37007,14 +37007,14 @@ export interface components {
3700737007
body_html: string;
3700837008
created_at: string;
3700937009
description: string;
37010-
docker_metadata?: (Record<string, never>)[];
37010+
docker_metadata?: (unknown)[];
3701137011
draft?: boolean;
3701237012
/** Format: uri */
3701337013
html_url: string;
3701437014
id: number;
3701537015
installation_command: string;
3701637016
manifest?: string;
37017-
metadata: (Record<string, never>)[];
37017+
metadata: (unknown)[];
3701837018
name: string;
3701937019
package_files: ({
3702037020
content_type: string;
@@ -37083,7 +37083,7 @@ export interface components {
3708337083
/** Format: uri */
3708437084
url: string;
3708537085
};
37086-
rubygems_metadata?: (Record<string, never>)[];
37086+
rubygems_metadata?: (unknown)[];
3708737087
/** Format: uri */
3708837088
source_url?: string;
3708937089
summary: string;
@@ -69358,7 +69358,7 @@ export interface components {
6935869358
};
6935969359
created_at?: string;
6936069360
description: string;
69361-
docker_metadata?: (Record<string, never>)[];
69361+
docker_metadata?: (unknown)[];
6936269362
draft?: boolean;
6936369363
html_url: string;
6936469364
id: number;
@@ -69464,7 +69464,7 @@ export interface components {
6946469464
target_commitish?: string;
6946569465
url?: string;
6946669466
};
69467-
rubygems_metadata?: (Record<string, never>)[];
69467+
rubygems_metadata?: (unknown)[];
6946869468
summary: string;
6946969469
tag_name?: string;
6947069470
target_commitish?: string;
@@ -69549,7 +69549,7 @@ export interface components {
6954969549
id: number;
6955069550
installation_command: string;
6955169551
manifest?: string;
69552-
metadata: (Record<string, never>)[];
69552+
metadata: (unknown)[];
6955369553
name: string;
6955469554
package_files: ({
6955569555
content_type?: string;
@@ -69598,7 +69598,7 @@ export interface components {
6959869598
target_commitish: string;
6959969599
url: string;
6960069600
};
69601-
rubygems_metadata?: (Record<string, never>)[];
69601+
rubygems_metadata?: (unknown)[];
6960269602
summary: string;
6960369603
tag_name?: string;
6960469604
target_commitish: string;
@@ -77716,9 +77716,9 @@ export interface operations {
7771677716
content?: string;
7771777717
/** @description The new filename for the file. */
7771877718
filename?: OneOf<[string, null]>;
77719-
} & (Record<string, never> | Record<string, never> | Record<string, never>), null]>) | undefined;
77719+
} & (unknown | unknown | Record<string, never>), null]>) | undefined;
7772077720
};
77721-
}) & (Record<string, never> | Record<string, never>), null]>;
77721+
}) & (unknown | unknown), null]>;
7772277722
};
7772377723
};
7772477724
responses: {
@@ -95046,7 +95046,7 @@ export interface operations {
9504695046
*/
9504795047
path: "/" | "/docs";
9504895048
});
95049-
}) & (Record<string, never> | Record<string, never> | Record<string, never> | Record<string, never> | Record<string, never>);
95049+
}) & (unknown | unknown | unknown | unknown | unknown);
9505095050
};
9505195051
};
9505295052
responses: {
@@ -95089,7 +95089,7 @@ export interface operations {
9508995089
*/
9509095090
path?: "/" | "/docs";
9509195091
};
95092-
}) & (Record<string, never> | Record<string, never>), null]>;
95092+
}) & (unknown | unknown), null]>;
9509395093
};
9509495094
};
9509595095
responses: {
@@ -96101,7 +96101,7 @@ export interface operations {
9610196101
reviewers?: (string)[];
9610296102
/** @description An array of team `slug`s that will be requested. */
9610396103
team_reviewers?: (string)[];
96104-
} & (Record<string, never> | Record<string, never>);
96104+
} & (unknown | unknown);
9610596105
};
9610696106
};
9610796107
responses: {

examples/github-api.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -10226,8 +10226,8 @@ export interface components {
1022610226
comments_url: string;
1022710227
owner?: components["schemas"]["simple-user"];
1022810228
truncated?: boolean;
10229-
forks?: (Record<string, never>)[];
10230-
history?: (Record<string, never>)[];
10229+
forks?: (unknown)[];
10230+
history?: (unknown)[];
1023110231
};
1023210232
/**
1023310233
* Public User
@@ -10371,8 +10371,8 @@ export interface components {
1037110371
comments_url: string;
1037210372
owner?: components["schemas"]["nullable-simple-user"];
1037310373
truncated?: boolean;
10374-
forks?: (Record<string, never>)[];
10375-
history?: (Record<string, never>)[];
10374+
forks?: (unknown)[];
10375+
history?: (unknown)[];
1037610376
}) | null;
1037710377
url?: string;
1037810378
forks_url?: string;
@@ -21050,8 +21050,8 @@ export interface components {
2105021050
primary_key_id?: number;
2105121051
key_id?: string;
2105221052
public_key?: string;
21053-
emails?: (Record<string, never>)[];
21054-
subkeys?: (Record<string, never>)[];
21053+
emails?: (unknown)[];
21054+
subkeys?: (unknown)[];
2105521055
can_sign?: boolean;
2105621056
can_encrypt_comms?: boolean;
2105721057
can_encrypt_storage?: boolean;
@@ -38870,7 +38870,7 @@ export interface components {
3887038870
}) | null;
3887138871
created_at?: string;
3887238872
description: string;
38873-
docker_metadata?: (Record<string, never>)[];
38873+
docker_metadata?: (unknown)[];
3887438874
draft?: boolean;
3887538875
/** Format: uri */
3887638876
html_url: string;
@@ -38997,7 +38997,7 @@ export interface components {
3899738997
/** Format: uri */
3899838998
url: string;
3899938999
};
39000-
rubygems_metadata?: (Record<string, never>)[];
39000+
rubygems_metadata?: (unknown)[];
3900139001
source_url?: string;
3900239002
summary: string;
3900339003
tag_name?: string;
@@ -39117,14 +39117,14 @@ export interface components {
3911739117
body_html: string;
3911839118
created_at: string;
3911939119
description: string;
39120-
docker_metadata?: (Record<string, never>)[];
39120+
docker_metadata?: (unknown)[];
3912139121
draft?: boolean;
3912239122
/** Format: uri */
3912339123
html_url: string;
3912439124
id: number;
3912539125
installation_command: string;
3912639126
manifest?: string;
39127-
metadata: (Record<string, never>)[];
39127+
metadata: (unknown)[];
3912839128
name: string;
3912939129
package_files: ({
3913039130
content_type: string;
@@ -39193,7 +39193,7 @@ export interface components {
3919339193
/** Format: uri */
3919439194
url: string;
3919539195
};
39196-
rubygems_metadata?: (Record<string, never>)[];
39196+
rubygems_metadata?: (unknown)[];
3919739197
/** Format: uri */
3919839198
source_url?: string;
3919939199
summary: string;
@@ -71468,7 +71468,7 @@ export interface components {
7146871468
};
7146971469
created_at?: string;
7147071470
description: string;
71471-
docker_metadata?: (Record<string, never>)[];
71471+
docker_metadata?: (unknown)[];
7147271472
draft?: boolean;
7147371473
html_url: string;
7147471474
id: number;
@@ -71574,7 +71574,7 @@ export interface components {
7157471574
target_commitish?: string;
7157571575
url?: string;
7157671576
};
71577-
rubygems_metadata?: (Record<string, never>)[];
71577+
rubygems_metadata?: (unknown)[];
7157871578
summary: string;
7157971579
tag_name?: string;
7158071580
target_commitish?: string;
@@ -71659,7 +71659,7 @@ export interface components {
7165971659
id: number;
7166071660
installation_command: string;
7166171661
manifest?: string;
71662-
metadata: (Record<string, never>)[];
71662+
metadata: (unknown)[];
7166371663
name: string;
7166471664
package_files: ({
7166571665
content_type?: string;
@@ -71708,7 +71708,7 @@ export interface components {
7170871708
target_commitish: string;
7170971709
url: string;
7171071710
};
71711-
rubygems_metadata?: (Record<string, never>)[];
71711+
rubygems_metadata?: (unknown)[];
7171271712
summary: string;
7171371713
tag_name?: string;
7171471714
target_commitish: string;
@@ -79864,9 +79864,9 @@ export interface operations {
7986479864
content?: string;
7986579865
/** @description The new filename for the file. */
7986679866
filename?: string | null;
79867-
}) & (Record<string, never> | Record<string, never> | Record<string, never>)) | null) | undefined;
79867+
}) & (unknown | unknown | Record<string, never>)) | null) | undefined;
7986879868
};
79869-
}) & (Record<string, never> | Record<string, never>)) | null;
79869+
}) & (unknown | unknown)) | null;
7987079870
};
7987179871
};
7987279872
responses: {
@@ -97284,7 +97284,7 @@ export interface operations {
9728497284
*/
9728597285
path: "/" | "/docs";
9728697286
});
97287-
}) & (Record<string, never> | Record<string, never> | Record<string, never> | Record<string, never> | Record<string, never>);
97287+
}) & (unknown | unknown | unknown | unknown | unknown);
9728897288
};
9728997289
};
9729097290
responses: {
@@ -97327,7 +97327,7 @@ export interface operations {
9732797327
*/
9732897328
path?: "/" | "/docs";
9732997329
};
97330-
}) & (Record<string, never> | Record<string, never>)) | null;
97330+
}) & (unknown | unknown)) | null;
9733197331
};
9733297332
};
9733397333
responses: {
@@ -98344,7 +98344,7 @@ export interface operations {
9834498344
reviewers?: (string)[];
9834598345
/** @description An array of team `slug`s that will be requested. */
9834698346
team_reviewers?: (string)[];
98347-
} & (Record<string, never> | Record<string, never>);
98347+
} & (unknown | unknown);
9834898348
};
9834998349
};
9835098350
responses: {

examples/octokit-ghes-3.6-diff-to-api.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3539,13 +3539,13 @@ export interface components {
35393539
ldap?: {
35403540
host?: string | null;
35413541
port?: number;
3542-
base?: (Record<string, never>)[];
3542+
base?: (unknown)[];
35433543
uid?: string | null;
35443544
bind_dn?: string | null;
35453545
password?: string | null;
35463546
method?: string;
35473547
search_strategy?: string;
3548-
user_groups?: (Record<string, never>)[];
3548+
user_groups?: (unknown)[];
35493549
admin_group?: string | null;
35503550
virtual_attribute_enabled?: boolean;
35513551
recursive_group_search?: boolean;
@@ -9371,7 +9371,7 @@ export interface operations {
93719371
op: "add" | "Add" | "remove" | "Remove" | "replace" | "Replace";
93729372
path?: string;
93739373
/** @description Can be any value - string, number, array or object. */
9374-
value?: Record<string, never>;
9374+
value?: unknown;
93759375
})[];
93769376
};
93779377
};

src/transform/schema-object.ts

+3
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ export function defaultSchemaObjectTransform(
257257

258258
if (finalType) return finalType;
259259

260+
// any type
261+
if (!("type" in schemaObject)) return "unknown";
262+
260263
// if no type could be generated, fall back to “empty object” type
261264
return ctx.emptyObjectsUnknown ? "Record<string, unknown>" : "Record<string, never>";
262265
}

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ export type SchemaObject = {
455455
}
456456
| { allOf: (SchemaObject | ReferenceObject)[]; anyOf?: (SchemaObject | ReferenceObject)[]; required?: string[] }
457457
| { allOf?: (SchemaObject | ReferenceObject)[]; anyOf: (SchemaObject | ReferenceObject)[]; required?: string[] }
458+
| {}
458459
);
459460

460461
/**

src/utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ export function tsArrayOf(type: string): string {
157157

158158
/** X & Y & Z; */
159159
export function tsIntersectionOf(...types: string[]): string {
160+
types = types.filter((t) => t !== "unknown");
161+
if (types.length === 0) return "unknown";
160162
if (types.length === 1) return String(types[0]); // don’t add parentheses around one thing
161163
return types.map((t) => (TS_UNION_INTERSECTION_RE.test(t) ? `(${t})` : t)).join(" & ");
162164
}

test/schema-object.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,11 @@ describe("Schema Object", () => {
523523
});
524524
});
525525
});
526+
527+
test("unknown", () => {
528+
const generated = transformSchemaObject({}, options);
529+
expect(generated).toBe(`unknown`);
530+
});
526531
});
527532

528533
describe("ReferenceObject", () => {

0 commit comments

Comments
 (0)