Skip to content

Commit 4685dd1

Browse files
authored
Add type annotations to schema parser chain (#1610)
Type the core schema parser classes to reduce implicit any and remove unnecessary @ts-expect-error directives: - MonoSchemaParser: annotate typeName, schemaPath, and constructor params; define and export SchemaParserConfig interface - SchemaParser: annotate properties and constructor params, removing the TS(2525) suppression for the default parameter value - SchemaParserFabric: type createSchemaParser, createSchema, and createParsedComponent params; fix incorrect schema: string annotations in parseSchema/getInlineParseContent/getParseContent; remove TS(2345) suppression in createSchema - EnumSchemaParser: use ConstructorParameters for the spread constructor, removing TS(2556); type formatEnumKey parameter, removing TS(2345) - PrimitiveSchemaParser: narrow recordKeysContent/recordValuesContent from any to string - ObjectSchemaParser: narrow interfaceKeysContent from any to string
1 parent b72bfc8 commit 4685dd1

File tree

6 files changed

+56
-26
lines changed

6 files changed

+56
-26
lines changed

src/schema-parser/base-schema-parsers/enum.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { EnumKeyResolver } from "../util/enum-key-resolver.js";
66
export class EnumSchemaParser extends MonoSchemaParser {
77
enumKeyResolver: EnumKeyResolver;
88

9-
constructor(...args) {
10-
// @ts-expect-error TS(2556) FIXME: A spread argument must either have a tuple type or... Remove this comment to see the full error message
9+
constructor(...args: ConstructorParameters<typeof MonoSchemaParser>) {
1110
super(...args);
1211
this.enumKeyResolver = new EnumKeyResolver(this.config, []);
1312
}
@@ -129,7 +128,6 @@ export class EnumSchemaParser extends MonoSchemaParser {
129128
} else {
130129
content = this.schema.enum.map((value, index) => {
131130
return {
132-
// @ts-expect-error TS(2345) FIXME: Argument of type '{ value: any; }' is not assignab... Remove this comment to see the full error message
133131
key: this.formatEnumKey({ value }),
134132
type: keyType,
135133
value: formatValue(value),
@@ -157,7 +155,7 @@ export class EnumSchemaParser extends MonoSchemaParser {
157155
};
158156
}
159157

160-
formatEnumKey = ({ key, value }) => {
158+
formatEnumKey = ({ key, value }: { key?: string; value: unknown }) => {
161159
let formatted: string | undefined;
162160

163161
if (key) {

src/schema-parser/base-schema-parsers/object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class ObjectSchemaParser extends MonoSchemaParser {
9191
if (additionalProperties) {
9292
const propertyNamesSchema =
9393
this.schemaUtils.getSchemaPropertyNamesSchema(schema);
94-
let interfaceKeysContent: any;
94+
let interfaceKeysContent: string;
9595

9696
if (propertyNamesSchema) {
9797
interfaceKeysContent = this.schemaParserFabric

src/schema-parser/base-schema-parsers/primitive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export class PrimitiveSchemaParser extends MonoSchemaParser {
1212
this.schema,
1313
);
1414

15-
let recordKeysContent: any;
16-
let recordValuesContent: any;
15+
let recordKeysContent: string;
16+
let recordValuesContent: string;
1717

1818
if (propertyNamesSchema) {
1919
recordKeysContent = this.schemaParserFabric

src/schema-parser/mono-schema-parser.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ import type { SchemaParser } from "./schema-parser.js";
66
import type { SchemaParserFabric } from "./schema-parser-fabric.js";
77
import type { SchemaUtils } from "./schema-utils.js";
88

9+
export interface SchemaParserConfig {
10+
typeName?: string | null;
11+
// biome-ignore lint/suspicious/noExplicitAny: TODO: narrow to OpenAPI schema type
12+
schema?: any;
13+
schemaPath?: string[];
14+
}
15+
916
export class MonoSchemaParser {
10-
schema;
11-
typeName;
12-
schemaPath;
17+
// biome-ignore lint/suspicious/noExplicitAny: TODO: narrow to OpenAPI schema type
18+
schema: any;
19+
typeName: string | null;
20+
schemaPath: string[];
1321

1422
schemaParser: SchemaParser;
1523
schemaParserFabric: SchemaParserFabric;
@@ -21,9 +29,9 @@ export class MonoSchemaParser {
2129

2230
constructor(
2331
schemaParser: SchemaParser,
24-
schema,
25-
typeName = null,
26-
schemaPath = [],
32+
schema: unknown,
33+
typeName: string | null = null,
34+
schemaPath: string[] = [],
2735
) {
2836
this.schemaParser = schemaParser;
2937
this.schemaParserFabric = schemaParser.schemaParserFabric;

src/schema-parser/schema-parser-fabric.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { SchemaComponentsMap } from "../schema-components-map.js";
1010
import type { SchemaWalker } from "../schema-walker.js";
1111
import type { TemplatesWorker } from "../templates-worker.js";
1212
import type { TypeNameFormatter } from "../type-name-formatter.js";
13+
import type { SchemaParserConfig } from "./mono-schema-parser.js";
1314
import { SchemaFormatters } from "./schema-formatters.js";
1415
import { SchemaParser } from "./schema-parser.js";
1516
import { SchemaUtils } from "./schema-utils.js";
@@ -39,7 +40,11 @@ export class SchemaParserFabric {
3940
this.schemaFormatters = new SchemaFormatters(this);
4041
}
4142

42-
createSchemaParser = ({ schema, typeName, schemaPath }) => {
43+
createSchemaParser = ({
44+
schema,
45+
typeName,
46+
schemaPath,
47+
}: SchemaParserConfig) => {
4348
return new SchemaParser(this, { schema, typeName, schemaPath });
4449
};
4550

@@ -49,8 +54,13 @@ export class SchemaParserFabric {
4954
linkedComponent,
5055
schemaPath,
5156
...otherSchemaProps
57+
}: {
58+
content: unknown;
59+
linkedSchema?: Record<string, unknown>;
60+
linkedComponent?: SchemaComponent;
61+
schemaPath?: string[];
62+
[key: string]: unknown;
5263
}) => {
53-
// @ts-expect-error TS(2345) FIXME: Argument of type '{ schema: any; schemaPath: any; ... Remove this comment to see the full error message
5464
const parser = this.createSchemaParser({
5565
schema: linkedComponent || linkedSchema,
5666
schemaPath,
@@ -68,7 +78,7 @@ export class SchemaParserFabric {
6878
typeName,
6979
schema,
7080
schemaPath,
71-
}): SchemaComponent => {
81+
}: Required<SchemaParserConfig> & { typeName: string }): SchemaComponent => {
7282
const schemaCopy = structuredClone(schema);
7383
const customComponent = this.schemaComponentsMap.createComponent(
7484
this.schemaComponentsMap.createRef(["components", "schemas", typeName]),
@@ -83,7 +93,7 @@ export class SchemaParserFabric {
8393
};
8494

8595
parseSchema = (
86-
schema: string,
96+
schema: SchemaParserConfig["schema"],
8797
typeName: string | null = null,
8898
schemaPath: string[] = [],
8999
): ParsedSchema<
@@ -98,19 +108,21 @@ export class SchemaParserFabric {
98108
};
99109

100110
getInlineParseContent = (
101-
schema: string,
111+
schema: SchemaParserConfig["schema"],
102112
typeName: string | null,
103113
schemaPath: string[],
104-
): Record<string, any> => {
114+
): // biome-ignore lint/suspicious/noExplicitAny: TODO: narrow return type
115+
Record<string, any> => {
105116
const parser = this.createSchemaParser({ schema, typeName, schemaPath });
106117
return parser.getInlineParseContent();
107118
};
108119

109120
getParseContent = (
110-
schema: string,
121+
schema: SchemaParserConfig["schema"],
111122
typeName: string | null,
112123
schemaPath: string[],
113-
): Record<string, any> => {
124+
): // biome-ignore lint/suspicious/noExplicitAny: TODO: narrow return type
125+
Record<string, any> => {
114126
const parser = this.createSchemaParser({ schema, typeName, schemaPath });
115127
return parser.getParseContent();
116128
};

src/schema-parser/schema-parser.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,24 @@ export class SchemaParser {
3232
templatesWorker: TemplatesWorker;
3333
schemaWalker: SchemaWalker;
3434

35-
typeName;
36-
schema;
37-
schemaPath = [];
35+
typeName: string | null;
36+
// biome-ignore lint/suspicious/noExplicitAny: TODO: narrow to OpenAPI schema type
37+
schema: any;
38+
schemaPath: string[];
3839

39-
// @ts-expect-error TS(2525) FIXME: Initializer provides no value for this binding ele... Remove this comment to see the full error message
40-
constructor(schemaParserFabric, { typeName, schema, schemaPath } = {}) {
40+
constructor(
41+
schemaParserFabric: SchemaParserFabric,
42+
{
43+
typeName,
44+
schema,
45+
schemaPath,
46+
}: {
47+
typeName?: string | null;
48+
// biome-ignore lint/suspicious/noExplicitAny: TODO: narrow to OpenAPI schema type
49+
schema?: any;
50+
schemaPath?: string[];
51+
} = {},
52+
) {
4153
this.schemaParserFabric = schemaParserFabric;
4254
this.config = schemaParserFabric.config;
4355
this.templatesWorker = schemaParserFabric.templatesWorker;

0 commit comments

Comments
 (0)