Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove suppressImplicitAnyIndexErrors from tsconfig #593

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"emitDecoratorMetadata": true,
"declaration": true,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
Expand All @@ -20,7 +19,6 @@
"preserveConstEnums": true,
"sourceMap": true,
"typeRoots" : ["node_modules/@types"],
"ignoreDeprecations": "5.0"
},
"include": [
"**/*.ts",
Expand Down
58 changes: 33 additions & 25 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ export interface Definition extends Omit<JSONSchema7, RedefinedFields> {
};
}

/** A looser Definition type that allows for indexing with arbitrary strings. */
type DefinitionIndex = { [key: string]: Definition[keyof Definition] };

export type SymbolRef = {
name: string;
typeName: string;
Expand Down Expand Up @@ -181,7 +184,7 @@ function extend(target: any, ..._: any[]): any {
}

function unique(arr: string[]): string[] {
const temp = {};
const temp: Record<string, true> = {};
for (const e of arr) {
temp[e] = true;
}
Expand Down Expand Up @@ -284,7 +287,7 @@ function resolveTupleType(propertyType: ts.Type): ts.TupleTypeNode | null {
return propertyType as any;
}

const simpleTypesAllowedProperties = {
const simpleTypesAllowedProperties: Record<string, true> = {
type: true,
description: true,
};
Expand Down Expand Up @@ -328,11 +331,11 @@ function makeNullable(def: Definition): Definition {
if (union) {
union.push({ type: "null" });
} else {
const subdef = {};
for (var k in def) {
const subdef: DefinitionIndex = {};
for (var k in def as any) {
if (def.hasOwnProperty(k)) {
subdef[k] = def[k];
delete def[k];
subdef[k] = def[k as keyof Definition];
delete def[k as keyof typeof def];
}
}
def.anyOf = [subdef, { type: "null" }];
Expand Down Expand Up @@ -441,7 +444,7 @@ const annotationKeywords: { [k in keyof typeof validationKeywords]?: true } = {
$ref: true,
};

const subDefinitions = {
const subDefinitions: Record<string, true> = {
items: true,
additionalProperties: true,
contains: true,
Expand Down Expand Up @@ -550,7 +553,7 @@ export class JsonSchemaGenerator {
/**
* Parse the comments of a symbol into the definition and other annotations.
*/
private parseCommentsIntoDefinition(symbol: ts.Symbol, definition: Definition, otherAnnotations: {}): void {
private parseCommentsIntoDefinition(symbol: ts.Symbol, definition: Definition, otherAnnotations: Record<string, true>): void {
if (!symbol) {
return;
}
Expand Down Expand Up @@ -613,24 +616,25 @@ export class JsonSchemaGenerator {
if (match) {
const k = match[1];
const v = match[2];
definition[name] = { ...definition[name], [k]: v ? parseValue(symbol, k, v) : true };
(definition as DefinitionIndex)[name] = { ...(definition as Record<string, Record<string, unknown>>)[name], [k]: v ? parseValue(symbol, k, v) : true };
return;
}
}

// In TypeScript 3.7+, the "." is kept as part of the annotation name
if (name.includes(".")) {
const parts = name.split(".");
if (parts.length === 2 && subDefinitions[parts[0]]) {
definition[parts[0]] = {
...definition[parts[0]],
const key = parts[0] as keyof Definition;
if (parts.length === 2 && subDefinitions[key]) {
(definition as DefinitionIndex)[key] = {
...definition[key] as Record<string, unknown>,
[parts[1]]: text ? parseValue(symbol, name, text) : true,
};
}
}

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

private getUnionDefinition(
unionType: ts.UnionType,
unionModifier: string,
unionModifier: keyof Definition,
definition: Definition
): Definition {
const enumValues: PrimitiveType[] = [];
Expand Down Expand Up @@ -1030,11 +1034,11 @@ export class JsonSchemaGenerator {
// If we already have a more specific description, don't overwrite it.
continue;
}
definition[k] = schemas[0][k];
(definition as DefinitionIndex)[k] = schemas[0][k as keyof Definition];
}
}
} else {
definition[unionModifier] = schemas;
(definition as DefinitionIndex)[unionModifier] = schemas;
}
return definition;
}
Expand Down Expand Up @@ -1070,7 +1074,7 @@ export class JsonSchemaGenerator {
if (schemas.length === 1) {
for (const k in schemas[0]) {
if (schemas[0].hasOwnProperty(k)) {
definition[k] = schemas[0][k];
(definition as DefinitionIndex)[k] = schemas[0][k as keyof Definition];
}
}
} else {
Expand Down Expand Up @@ -1175,7 +1179,7 @@ export class JsonSchemaGenerator {
}
}

const propertyDefinitions = props.reduce((all, prop) => {
const propertyDefinitions = props.reduce<Record<string, Definition>>((all, prop) => {
const propertyName = prop.getName();
const propDef = this.getDefinitionForProperty(prop, node);
if (propDef != null) {
Expand Down Expand Up @@ -1274,7 +1278,7 @@ export class JsonSchemaGenerator {
private getTypeDefinition(
typ: ts.Type,
asRef = this.args.ref,
unionModifier: string = "anyOf",
unionModifier: keyof Definition = "anyOf",
prop?: ts.Symbol,
reffedType?: ts.Symbol,
pairedSymbol?: ts.Symbol,
Expand Down Expand Up @@ -1400,7 +1404,7 @@ export class JsonSchemaGenerator {
}

// Parse comments
const otherAnnotations = {};
const otherAnnotations: Record<string, true> = {};
this.parseCommentsIntoDefinition(reffedType!, definition, otherAnnotations); // handle comments in the type alias declaration
this.parseCommentsIntoDefinition(symbol!, definition, otherAnnotations);
this.parseCommentsIntoDefinition(typ.aliasSymbol!, definition, otherAnnotations);
Expand Down Expand Up @@ -1492,8 +1496,8 @@ export class JsonSchemaGenerator {
this.recursiveTypeRef.delete(fullTypeName);
// If the type was recursive (there is reffedDefinitions) - lets replace it to reference
if (this.reffedDefinitions[fullTypeName]) {
const annotations = Object.entries(returnedDefinition).reduce((acc, [key, value]) => {
if (annotationKeywords[key] && typeof value !== undefined) {
const annotations = Object.entries(returnedDefinition).reduce<Record<string, unknown>>((acc, [key, value]) => {
if (annotationKeywords[key as keyof typeof annotationKeywords] && typeof value !== undefined) {
acc[key] = value;
}
return acc;
Expand Down Expand Up @@ -1551,7 +1555,11 @@ export class JsonSchemaGenerator {
}

public getSchemaForSymbols(symbolNames: string[], includeReffedDefinitions: boolean = true, includeAllOverrides: boolean = false): Definition {
const root = {
const root: {
$id?: string,
$schema: string,
definitions: Record<string, Definition>
} = {
$schema: "http://json-schema.org/draft-07/schema#",
definitions: {},
};
Expand Down Expand Up @@ -1660,7 +1668,7 @@ export function buildGenerator(

for (const pref in args) {
if (args.hasOwnProperty(pref)) {
settings[pref] = args[pref];
(settings as Record<string, Partial<Args>[keyof Args]>)[pref as keyof Args] = args[pref as keyof Args];
}
}

Expand Down
Loading