Skip to content

fix(plugin): declare typescript as an optional peer dependency - #4144

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
abouroubi:fix/plugin-typescript-peer-dependency
Sep 22, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
abouroubi:fix/plugin-typescript-peer-dependency

Conversation

@abouroubi

Copy link
Copy Markdown

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #4139

The CLI plugin imports typescript at runtime, but @nestjs/graphql does not declare it anywhere in its package.json. Nothing tells the package manager which copy the plugin should get, so it takes whatever is reachable from its install location. In a pnpm workspace with more than one TypeScript around, that can be a different version than the one @nestjs/cli compiles with.

The plugin inspects the compiler's types but compares type.flags against the TypeFlags of its own copy, and TypeScript 6 renumbered them:

5.9.3 6.0.3
String 4 32
Number 8 64
Enum 32 65536

So with the program on 6.0.3 and the plugin on 5.9.3, a string property reads as an enum and the generated metadata refers to a variable called string:

static _GRAPHQL_METADATA_FACTORY() {
    return { name: { type: () => string }, age: { type: () => Number } };
}

The build succeeds and the app throws ReferenceError: string is not defined when the schema is built.

What is the new behavior?

typescript is declared as an optional peer dependency, so the package manager resolves it from the application instead of leaving it to hoisting. Verified with the reproduction from #4139 (sibling workspace package on 5.9.3, application on 6.0.3, 5.9.3 hoisted):

                      plugin resolves typescript   emitted metadata
14.0.1                5.9.3 (hoisted)              name: { type: () => string }
with this change      6.0.3 (the application's)    name: { type: () => String }

The range is ^5.5.0 || ^6.0.0. 5.5 is the floor because the package is ESM: on 5.4 and below the named imports in plugin/utils/ast-utils.ts (import { TypeFlags, ... } from 'typescript') fail with does not provide an export named. Marking it optional follows ts-morph, which is plugin-only in the same way. Same fix as nestjs/swagger#4134.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Closes #4139.

@kamilmysliwiec
kamilmysliwiec merged commit 6267638 into nestjs:master Sep 22, 2026
1 check passed
@abouroubi
abouroubi deleted the fix/plugin-typescript-peer-dependency branch September 22, 2026 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI plugin imports typescript without declaring it and can load a different version than the compiler, emitting type: () => string

2 participants