Repository: vercel/next.js
File: packages/next/src/build/type-check.ts (TypeScript)

Code before:
  pagesDir: string | undefined,
  debugBuildPaths: { app: string[]; pages: string[] } | undefined
) {
  let impl: typeof verifyAndRunTypeScriptImpl
  let typeCheckWorker:
    | (Worker & {
        verifyAndRunTypeScript: typeof verifyAndRunTypeScriptImpl
      })
    | undefined
  if (shouldRunTypeCheck) {
    typeCheckWorker = new Worker(
      require.resolve('../lib/verify-typescript-setup'),
      {
        exposedMethods: ['verifyAndRunTypeScript'],
        debuggerPortOffset: -1,
        isolatedMemory: false,
        numWorkers: 1,
        enableWorkerThreads,
        maxRetries: 0,
      }
    ) as typeof typeCheckWorker
    impl = typeCheckWorker!.verifyAndRunTypeScript
  } else {
    // When not running typecheck, just run the implementation in-process without spawning a worker,
    // to avoid the overhead of the worker.
    impl = verifyAndRunTypeScriptImpl
  }

  return impl({
    dir,
    distDir,
    strictRouteTypes,
    shouldRunTypeCheck,
    tsconfigPath,
    typedRoutes,
    disableStaticImages,
    cacheDir,
    hasAppDir,
    hasPagesDir,
    appDir,
    pagesDir,
    debugBuildPaths,
  })
    .then((result) => {
      typeCheckWorker?.end()
      return result
    })
    .catch(() => {
      // The error is already logged in the worker, we simply exit the main thread to prevent the
      // `Jest worker encountered 1 child process exceptions, exceeding retry limit` from showing up
      process.exit(1)

Review comment (suggestion):
You'll want to `require` it here I think. Don't want to load it if no typescript (though unlikely nowadays).

Code after:
  debugBuildPaths: { app: string[]; pages: string[] } | undefined
) {
  let impl: typeof import('../lib/verify-typescript-setup').verifyAndRunTypeScript
  let typeCheckWorker:
    | (Worker & {
        verifyAndRunTypeScript: typeof impl
      })
    | undefined
  if (shouldRunTypeCheck) {
    typeCheckWorker = new Worker(
      require.resolve('../lib/verify-typescript-setup'),
      {
        exposedMethods: ['verifyAndRunTypeScript'],
        debuggerPortOffset: -1,
        isolatedMemory: false,
        numWorkers: 1,
        enableWorkerThreads,
        maxRetries: 0,
      }
    ) as typeof typeCheckWorker
    impl = typeCheckWorker!.verifyAndRunTypeScript
  } else {
    // When not running typecheck, just run the implementation in-process without spawning a worker,
    // to avoid the overhead of the worker.
    impl = (
      require('../lib/verify-typescript-setup') as typeof import('../lib/verify-typescript-setup')
    ).verifyAndRunTypeScript
  }

  return impl({
    dir,
    distDir,
    strictRouteTypes,
    shouldRunTypeCheck,
    tsconfigPath,
    typedRoutes,
    disableStaticImages,
    cacheDir,
    hasAppDir,
    hasPagesDir,
    appDir,
    pagesDir,
    debugBuildPaths,
  })
    .then((result) => {
      typeCheckWorker?.end()
      return result
    })
    .catch(() => {
      // The error is already logged in the worker, we simply exit the main thread to prevent the
      // `Jest worker encountered 1 child process exceptions, exceeding retry limit` from showing up