OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.51k stars 6.27k forks source link

[BUG] TypeScript (fetch) client causes TS7053 #19011

Open falk-stefan opened 4 days ago

falk-stefan commented 4 days ago

It looks like the previous issue I had (https://github.com/OpenAPITools/openapi-generator/issues/18446) was resolved, but, unfortunately, I am still not able to upgrade.

My local setup give me the following type error:

TS7053: Element implicitly has an any type because expression of type string can't be used to index type

The reason is that key is ont being cast to its correct type:

export function instanceOfAuthError(value: any): boolean {
    for (const key in AuthError) {
        if (Object.prototype.hasOwnProperty.call(AuthError, key)) {
            if (AuthError[key] === value) {
                ^^^^^^^^^^^^^^^^^^^^^^^^
                return true;
            }
        }
    }
    return false;
}

an explicit cast would be necessary:

if (AuthError[key as keyof typeof AuthError] === value) {
  return true;
}

openapi.json

{
  "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "7.6.0"
  }
}

Generated model AuthError.ts (full)

/* tslint:disable */
/* eslint-disable */
/**
 * backend-sequelize
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 0.0.1
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

/**
 *
 * @export
 */
export const AuthError = {
  AuthTokenExpired: 'auth/token-expired',
} as const;
export type AuthError = (typeof AuthError)[keyof typeof AuthError];

export function instanceOfAuthError(value: any): boolean {
  for (const key in AuthError) {
    if (Object.prototype.hasOwnProperty.call(AuthError, key)) {
      if (AuthError[key] === value) {
        return true;
      }
    }
  }
  return false;
}

export function AuthErrorFromJSON(json: any): AuthError {
  return AuthErrorFromJSONTyped(json, false);
}

export function AuthErrorFromJSONTyped(json: any, ignoreDiscriminator: boolean): AuthError {
  return json as AuthError;
}

export function AuthErrorToJSON(value?: AuthError | null): any {
  return value as any;
}