Skip to content

Commit 264ab45

Browse files
authored
Remove suppressImplicitAnyIndexErrors. (#593)
1 parent 2bfec01 commit 264ab45

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

tsconfig.json

-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"emitDecoratorMetadata": true,
1010
"declaration": true,
1111
"noImplicitAny": true,
12-
"suppressImplicitAnyIndexErrors": true,
1312
"strictNullChecks": true,
1413
"noImplicitReturns": true,
1514
"noFallthroughCasesInSwitch": true,
@@ -20,7 +19,6 @@
2019
"preserveConstEnums": true,
2120
"sourceMap": true,
2221
"typeRoots" : ["node_modules/@types"],
23-
"ignoreDeprecations": "5.0"
2422
},
2523
"include": [
2624
"**/*.ts",

typescript-json-schema.ts

+33-25
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ export interface Definition extends Omit<JSONSchema7, RedefinedFields> {
149149
};
150150
}
151151

152+
/** A looser Definition type that allows for indexing with arbitrary strings. */
153+
type DefinitionIndex = { [key: string]: Definition[keyof Definition] };
154+
152155
export type SymbolRef = {
153156
name: string;
154157
typeName: string;
@@ -181,7 +184,7 @@ function extend(target: any, ..._: any[]): any {
181184
}
182185

183186
function unique(arr: string[]): string[] {
184-
const temp = {};
187+
const temp: Record<string, true> = {};
185188
for (const e of arr) {
186189
temp[e] = true;
187190
}
@@ -284,7 +287,7 @@ function resolveTupleType(propertyType: ts.Type): ts.TupleTypeNode | null {
284287
return propertyType as any;
285288
}
286289

287-
const simpleTypesAllowedProperties = {
290+
const simpleTypesAllowedProperties: Record<string, true> = {
288291
type: true,
289292
description: true,
290293
};
@@ -328,11 +331,11 @@ function makeNullable(def: Definition): Definition {
328331
if (union) {
329332
union.push({ type: "null" });
330333
} else {
331-
const subdef = {};
332-
for (var k in def) {
334+
const subdef: DefinitionIndex = {};
335+
for (var k in def as any) {
333336
if (def.hasOwnProperty(k)) {
334-
subdef[k] = def[k];
335-
delete def[k];
337+
subdef[k] = def[k as keyof Definition];
338+
delete def[k as keyof typeof def];
336339
}
337340
}
338341
def.anyOf = [subdef, { type: "null" }];
@@ -441,7 +444,7 @@ const annotationKeywords: { [k in keyof typeof validationKeywords]?: true } = {
441444
$ref: true,
442445
};
443446

444-
const subDefinitions = {
447+
const subDefinitions: Record<string, true> = {
445448
items: true,
446449
additionalProperties: true,
447450
contains: true,
@@ -550,7 +553,7 @@ export class JsonSchemaGenerator {
550553
/**
551554
* Parse the comments of a symbol into the definition and other annotations.
552555
*/
553-
private parseCommentsIntoDefinition(symbol: ts.Symbol, definition: Definition, otherAnnotations: {}): void {
556+
private parseCommentsIntoDefinition(symbol: ts.Symbol, definition: Definition, otherAnnotations: Record<string, true>): void {
554557
if (!symbol) {
555558
return;
556559
}
@@ -613,24 +616,25 @@ export class JsonSchemaGenerator {
613616
if (match) {
614617
const k = match[1];
615618
const v = match[2];
616-
definition[name] = { ...definition[name], [k]: v ? parseValue(symbol, k, v) : true };
619+
(definition as DefinitionIndex)[name] = { ...(definition as Record<string, Record<string, unknown>>)[name], [k]: v ? parseValue(symbol, k, v) : true };
617620
return;
618621
}
619622
}
620623

621624
// In TypeScript 3.7+, the "." is kept as part of the annotation name
622625
if (name.includes(".")) {
623626
const parts = name.split(".");
624-
if (parts.length === 2 && subDefinitions[parts[0]]) {
625-
definition[parts[0]] = {
626-
...definition[parts[0]],
627+
const key = parts[0] as keyof Definition;
628+
if (parts.length === 2 && subDefinitions[key]) {
629+
(definition as DefinitionIndex)[key] = {
630+
...definition[key] as Record<string, unknown>,
627631
[parts[1]]: text ? parseValue(symbol, name, text) : true,
628632
};
629633
}
630634
}
631635

632-
if (validationKeywords[name] || this.userValidationKeywords[name]) {
633-
definition[name] = text === undefined ? "" : parseValue(symbol, name, text);
636+
if (validationKeywords[name as keyof typeof validationKeywords] || this.userValidationKeywords[name]) {
637+
(definition as DefinitionIndex)[name] = text === undefined ? "" : parseValue(symbol, name, text);
634638
} else {
635639
// special annotations
636640
otherAnnotations[doc.name] = true;
@@ -939,7 +943,7 @@ export class JsonSchemaGenerator {
939943

940944
private getUnionDefinition(
941945
unionType: ts.UnionType,
942-
unionModifier: string,
946+
unionModifier: keyof Definition,
943947
definition: Definition
944948
): Definition {
945949
const enumValues: PrimitiveType[] = [];
@@ -1030,11 +1034,11 @@ export class JsonSchemaGenerator {
10301034
// If we already have a more specific description, don't overwrite it.
10311035
continue;
10321036
}
1033-
definition[k] = schemas[0][k];
1037+
(definition as DefinitionIndex)[k] = schemas[0][k as keyof Definition];
10341038
}
10351039
}
10361040
} else {
1037-
definition[unionModifier] = schemas;
1041+
(definition as DefinitionIndex)[unionModifier] = schemas;
10381042
}
10391043
return definition;
10401044
}
@@ -1070,7 +1074,7 @@ export class JsonSchemaGenerator {
10701074
if (schemas.length === 1) {
10711075
for (const k in schemas[0]) {
10721076
if (schemas[0].hasOwnProperty(k)) {
1073-
definition[k] = schemas[0][k];
1077+
(definition as DefinitionIndex)[k] = schemas[0][k as keyof Definition];
10741078
}
10751079
}
10761080
} else {
@@ -1175,7 +1179,7 @@ export class JsonSchemaGenerator {
11751179
}
11761180
}
11771181

1178-
const propertyDefinitions = props.reduce((all, prop) => {
1182+
const propertyDefinitions = props.reduce<Record<string, Definition>>((all, prop) => {
11791183
const propertyName = prop.getName();
11801184
const propDef = this.getDefinitionForProperty(prop, node);
11811185
if (propDef != null) {
@@ -1274,7 +1278,7 @@ export class JsonSchemaGenerator {
12741278
private getTypeDefinition(
12751279
typ: ts.Type,
12761280
asRef = this.args.ref,
1277-
unionModifier: string = "anyOf",
1281+
unionModifier: keyof Definition = "anyOf",
12781282
prop?: ts.Symbol,
12791283
reffedType?: ts.Symbol,
12801284
pairedSymbol?: ts.Symbol,
@@ -1400,7 +1404,7 @@ export class JsonSchemaGenerator {
14001404
}
14011405

14021406
// Parse comments
1403-
const otherAnnotations = {};
1407+
const otherAnnotations: Record<string, true> = {};
14041408
this.parseCommentsIntoDefinition(reffedType!, definition, otherAnnotations); // handle comments in the type alias declaration
14051409
this.parseCommentsIntoDefinition(symbol!, definition, otherAnnotations);
14061410
this.parseCommentsIntoDefinition(typ.aliasSymbol!, definition, otherAnnotations);
@@ -1492,8 +1496,8 @@ export class JsonSchemaGenerator {
14921496
this.recursiveTypeRef.delete(fullTypeName);
14931497
// If the type was recursive (there is reffedDefinitions) - lets replace it to reference
14941498
if (this.reffedDefinitions[fullTypeName]) {
1495-
const annotations = Object.entries(returnedDefinition).reduce((acc, [key, value]) => {
1496-
if (annotationKeywords[key] && typeof value !== undefined) {
1499+
const annotations = Object.entries(returnedDefinition).reduce<Record<string, unknown>>((acc, [key, value]) => {
1500+
if (annotationKeywords[key as keyof typeof annotationKeywords] && typeof value !== undefined) {
14971501
acc[key] = value;
14981502
}
14991503
return acc;
@@ -1551,7 +1555,11 @@ export class JsonSchemaGenerator {
15511555
}
15521556

15531557
public getSchemaForSymbols(symbolNames: string[], includeReffedDefinitions: boolean = true, includeAllOverrides: boolean = false): Definition {
1554-
const root = {
1558+
const root: {
1559+
$id?: string,
1560+
$schema: string,
1561+
definitions: Record<string, Definition>
1562+
} = {
15551563
$schema: "http://json-schema.org/draft-07/schema#",
15561564
definitions: {},
15571565
};
@@ -1660,7 +1668,7 @@ export function buildGenerator(
16601668

16611669
for (const pref in args) {
16621670
if (args.hasOwnProperty(pref)) {
1663-
settings[pref] = args[pref];
1671+
(settings as Record<string, Partial<Args>[keyof Args]>)[pref as keyof Args] = args[pref as keyof Args];
16641672
}
16651673
}
16661674

0 commit comments

Comments
 (0)