TypeScript: "The inferred type of X cannot be named without a reference to Y" (TS2742) still happens, when working with npm link to link packages manually

🔎 Search Terms

is:issue is:open 2742

🕗 Version & Regression Information

This seems to not be a regression bug

⏯ Playground Link

https://github.com/Teascade/typescript-error-demonstration

💻 Code

// In the example provided
// https://github.com/Teascade/typescript-error-demonstration

import { extended } from '@privateprefix/lib';

// src/index.ts(4,14): error TS2742: The inferred type of 'a' cannot be named without a reference to '@privateprefix/lib/node_modules/joi'. This is likely not portable. A type annotation is necessary.
export const a = extended.object().keys();

Replacing this with

// Joi is declared, but never used ts(6133)
import { type Joi, extended } from '@privateprefix/lib';

// This works now, though
export const a = extended.object().keys();

🙁 Actual behavior

Produces src/index.ts(4,14): error TS2742: The inferred type of 'a' cannot be named without a reference to '@privateprefix/lib/node_modules/joi'. This is likely not portable. A type annotation is necessary.

🙂 Expected behavior

No error is to be expected

Additional information about the issue

In https://github.com/microsoft/TypeScript/issues/42873#issuecomment-2066874644 it was mentioned, that this issue would now be resolved in TypeScript beta 5.5.0, but it seems to still be broken in this specific scenario

I’ve made a minimal repository to reroduce this issue in https://github.com/Teascade/typescript-error-demonstration

The GitHub repository has a github action in which the issue is reproduced: https://github.com/Teascade/typescript-error-demonstration/actions/runs/9568149578/job/26377636108

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 7
  • Comments: 16 (2 by maintainers)

Most upvoted comments

for the me issue was with trpc - same issue as yours

import { initTRPC } from '@trpc/server'
import { createExpressMiddleware } from '@trpc/server/adapters/express'

export const t = initTRPC.create()

export const trpcRouter = t.router({
  // ...
})

export const expressAdapter = createExpressMiddleware({
  router: trpcRouter,
})

expressAdapter produced the issue. The solution ✔ was to explicitly install @types/express and the bundler could finally realize what types it was working with

Our fix for this relies on package.json files within the compilation folder to discover available dependencies not explicitly included via imports in the project. To avoid crawling the whole directory spine, we stop looking for package files at the project root. nx, however, seems to avoid using per-project package manifests, so we never find those deps, since they’re only declared in a folder above each project, which means you’re back to the old workaround: either annotate it yourself, like the error asks, or add an import "@nestjs/common"; to a file in the project to force us to discover the dep.

I am also encountering this issue and wanted to share additional context that might help in diagnosing the problem.

I have created a minimal reproduction repository that demonstrates the issue:
https://github.com/skrtheboss/reproduce-inferred-type-error

Steps to reproduce:

  1. Clone the repository.

  2. Install dependencies:

    pnpm install
    
  3. Run the command:

    pnpm nx run test-lib:build
    
  4. Observe the following error during TypeScript compilation:

    > nx run test-lib:build
    
    Compiling TypeScript files for project "test-lib"...
    libs/test-lib/src/index.ts:7:14 - error TS2742: The inferred type of 'BDTO' cannot be named without a reference to '.pnpm/@nestjs+common@10.4.13_bx7ns4a6im6ljmadd2c7gy34li/node_modules/@nestjs/common'. This is likely not portable. A type annotation is necessary.
    
    7 export class BDTO extends OmitType(ADTO, ['foo']) {}
                   ~~~~
    

Workaround: Adding the following line to the imports resolves the issue:

import type {} from '@nestjs/common';

Let me know if further details or tweaks to the reproduction are needed. I’d appreciate any guidance or updates regarding a potential fix. Thanks!

Seeing this as well with Bazel when setting up sandbox with symlinked node_modules. Only with 5.6.

It happens with some specific 3rd party packages like storybook.

import type { Meta } from "@storybook/react";

export const M: Meta = {};

export const M2 = M;

with error:

The inferred type of 'M2' cannot be named without a reference to
'<sandbox-path>/node_modules/storybook/core/types'.
This is likely not portable. A type annotation is necessary.ts(2742)

While I can solve it with export const M2: typeof M = M or : Meta I believe something broke in 5.6. The same code works in a non-symlinked node_modules environment.

Recently upgraded to 5.5.4 without any of these issues, when I installed 5.6.0-beta got a bunch of these. 300+ package pnpm monorepo. Don’t have a reproduction case but just noting that this was not a problem in 5.5.4.

It doesn’t really have anything to do with npm link - it’s just that we don’t see the Joi types reexported in @privateprefix/lib as an autogeneratable way to reference joi’s innards, hence why it works when you explicitly import the alias to Joi. (The tarball install works because npm hoists the install to the top level, rather than doing the strict dependency nesting the link does.) We do do a little bit of looking for reexports, but generally we look for complete reexports, where one module is another module via export = - a nested reexport like this, where you import a default and reexport it as a named member I just don’t think is an alias we current traverse through while looking for reachable ways to name the module. So this is probably fixable, but the logic’s going to be a bit weird, since we’ll be looking for module symbol aliases at… basically every symbol name everywhere (rather than just reachable filenames).