Skip to content

Error when migrating to typesrcipt v7 #348

Closed

Description

@essuraj

Type Error: Cannot read properties of undefined (reading 'Latest')
at isValidIdentifier (\node_modules\tsutils\util\util.js:610:68)
at \node_modules@openapi-codegen\typescript\lib\core\schemaToTypeAliasDeclaration.js:153:126
at Array.map ()
at getType (\node_modules@openapi-codegen\typescript\lib\core\schemaToTypeAliasDeclaration.js:149:69)
at schemaToTypeAliasDeclaration (\node_modules@openapi-codegen\typescript\lib\core\schemaToTypeAliasDeclaration.js:25:220)
at \node_modules@openapi-codegen\typescript\lib\generators\generateSchemaTypes.js:39:76
at Array.reduce ()
at handleTypeAlias (\node_modules@openapi-codegen\typescript\lib\generators\generateSchemaTypes.js:37:84)
at generateTypeAliasDeclarations (\node_modules@openapi-codegen\typescript\lib\generators\generateSchemaTypes.js:58:20)
at generateSchemaTypes (\node_modules@openapi-codegen\typescript\lib\generators\generateSchemaTypes.js:78:25)

Activity

  1. el-j commented on Oct 26, 2026

    @el-j
    Contributor

    TypeScript 7 Migration Findings, Compatibility Adapter & Roadmap

    We have conducted a thorough investigation into the TypeScript 7 migration path and explored the solution in draft PR #355 (feat/typescript-7-upgrade).


    1. Root Cause Analysis

    The error originally reported when running with TypeScript 7:

    TypeError: Cannot read properties of undefined (reading 'Latest')
        at isValidIdentifier (\node_modules\tsutils\util\util.js:610:68)
    

    occurs because:

    1. Generator plugins relied on tsutils, which calls ts.ScriptTarget.Latest on the root typescript import.
    2. In TypeScript 7.x (7.0.2 and 7.1.0-beta), Microsoft redesigned the package structure as part of the Go-based compiler rewrite (tracked upstream in microsoft/TypeScript#63703). The root entry point import ts from "typescript" only exports ./lib/version.cjs ({ version: "7.x" }). The entire compiler API namespace (ts.ScriptTarget, ts.SyntaxKind, ts.factory, ts.createPrinter, etc.) was removed from the package root.

    2. Key Findings from typescript@7.1.0-beta

    Testing against the latest typescript@7.1.0-beta reveals the new modular package architecture:

    • Modular Subpaths: AST utilities are now partitioned into distinct subpaths (typescript/ast, typescript/ast/factory, typescript/ast/is).
    • Factory Changes: Factory functions (createIdentifier, createCallExpression, etc.) are individual functions rather than methods on a unified ts.factory object. Functions have stricter signatures (e.g., createTypeAliasDeclaration requires an Identifier node rather than accepting raw strings).
    • Renamed Methods: e.g., createPropertySignature is renamed to createPropertySignatureDeclaration.
    • Tokens: createModifier(kind) was removed in favor of createToken(kind).
    • Native Emitter / Printer: TypeScript 7.1 delegates AST emitting to a native binary via an IPC RPC channel (typescript/sync). We verified that new sync.API().printer.printNode(...) is functional.

    3. Proposed Solution: The Backwards-Compatibility Adapter

    To prevent breaking existing custom generators or requiring a high-risk immediate rewrite of all 33 files that interact with the TypeScript compiler, we are using an Adapter Pattern:

    ┌────────────────────────────────────────────────────────┐
    │  Built-in Generators & Third-party Generator Plugins   │
    │  (expecting ts.*, ts.factory.*, ts.createPrinter)      │
    └───────────────────────────┬────────────────────────────┘
                                │
                                ▼
    ┌────────────────────────────────────────────────────────┐
    │   Compatibility Adapter (plugins/typescript/src/ts.ts) │
    │   - Emulates legacy ts namespace & ts.factory methods  │
    │   - Normalizes signatures (tokens, string->Identifier) │
    │   - Wraps printer via native sync.API().printer        │
    └──────────────┬───────────────────────────┬─────────────┘
                   │                           │
                   ▼                           ▼
    ┌──────────────────────────────┐ ┌───────────────────────┐
    │ TypeScript 7 Subpaths        │ │ Native IPC Binary     │
    │ (ast, ast/factory, ast/is)   │ │ (typescript/sync)     │
    └──────────────────────────────┘ └───────────────────────┘
    
    Why this is the best path:
    • Downstream Safety: Any external custom generator plugin continuing to import ts from openapi-codegen will retain backwards compatibility.
    • Incremental Modernization: Internal generators remain stable and fully testable during the migration.
    • Clean Fallback: Removes tsutils entirely and replaces it with an internal isValidIdentifier helper.

    4. Two-Phase Roadmap

    • Phase 1: Compatibility Adapter Layer & Green CI (Completed in PR WIP: TypeScript 7 / 7.1 migration #355)

      • Merge latest clean baseline from PR chore: update tooling #353 (all reviews resolved, CI 100% green).
      • Upgrade workspace to typescript@7.1.0-beta.
      • Replace tsutils with internal isValidIdentifier.
      • Implement adapter shim in plugins/typescript/src/typescript.ts (re-export AST nodes, SyntaxKind, enums, factory bridge).
      • Bridge createPrinter with new sync.API().printer and comment/JSDoc injection.
      • Adapt transformer APIs in cli/src/core/updateConfig.ts & InitCommand.ts.
      • Achieve green pnpm check:types and pnpm test --run (28/28 test suites, 243/243 tests pass, CI green).
    • Phase 2: Direct TS 7 Native Style Modernization (33 Tracked Items)
      Once Microsoft finalizes native comment/trivia printing in typescript/sync, the 33 files below can transition from the compatibility adapter to direct typescript/ast/* imports:

      • Generators (3 files)

        • plugins/typescript/src/generators/generateFetchers.ts
        • plugins/typescript/src/generators/generateReactQueryComponents.ts
        • plugins/typescript/src/generators/generateSchemaTypes.ts
      • Core AST Declaration Builders (12 files)

        • plugins/typescript/src/core/createNamedImport.ts
        • plugins/typescript/src/core/createNamespaceImport.ts
        • plugins/typescript/src/core/createOperationFetcherFnNodes.ts
        • plugins/typescript/src/core/createOperationQueryFnNodes.ts
        • plugins/typescript/src/core/createWatermark.ts
        • plugins/typescript/src/core/getDataResponseType.ts
        • plugins/typescript/src/core/getErrorResponseType.ts
        • plugins/typescript/src/core/getOperationTypes.ts
        • plugins/typescript/src/core/getRequestBodyType.ts
        • plugins/typescript/src/core/getVariablesType.ts
        • plugins/typescript/src/core/schemaToEnumDeclaration.ts
        • plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts
      • Core AST Utilities & Scanners (3 files)

        • plugins/typescript/src/core/analyzeImportUsage.ts
        • plugins/typescript/src/core/getUsedImports.ts
        • plugins/typescript/src/utils/isValidIdentifier.ts
      • CLI AST Transformers & Utilities (6 files)

        • cli/src/commands/InitCommand.ts
        • cli/src/core/generateConfigProperty.ts
        • cli/src/core/generateConfigProperty.test.ts
        • cli/src/core/updateConfig.ts
        • cli/src/core/updateConfig.test.ts
        • cli/src/utils/getText.ts
      • Plugin Test Suites & Test Helpers (8 files)

        • plugins/typescript/src/core/analyzeImportUsage.test.ts
        • plugins/typescript/src/core/createNamedImport.test.ts
        • plugins/typescript/src/core/getOperationTypes.test.ts
        • plugins/typescript/src/core/getUsedImports.test.ts
        • plugins/typescript/src/core/getVariablesType.test.ts
        • plugins/typescript/src/core/schemaToEnumDeclaration.test.ts
        • plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts
        • plugins/typescript/src/testUtils.ts
      • Adapter Deprecation & Sunset (1 file)

        • plugins/typescript/src/typescript.ts (deprecate and sunset once all modules import typescript/ast/* directly)

    Related PRs & Issues

  2. added a commit that references this issue on Nov 19, 2026
    478cad6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions