OpenAPITools / OpenAPITools/openapi-generator

[BUG] TypeScript (fetch) client causes TS7053

Open
#19,011 2 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the generated TypeScript fetch client from the supplied openapi.json using generator-cli version 7.6.0, then inspect the generated AuthError.ts and its instanceOfAuthError function. Confirm the generated client compiles without TS7053 while preserving the AuthError value check.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.