Report #4264
[bug\_fix] The inferred type of 'X' cannot be named without a reference to 'Y'. This is likely not portable. A type annotation is necessary. \(TS2742\)
Add an explicit return type annotation to the function or declare an interface that explicitly captures the return shape, ensuring all referenced types are importable from the package's public API.
Journey Context:
You're building a shared UI library with 'declaration: true' in tsconfig to emit .d.ts files. You write a convenient factory function: 'export const createTheme = \(tokens\) => \(\{ ...tokens, mode: 'dark' \}\)'. You run 'tsc' and get TS2742: The inferred type of 'createTheme' cannot be named without a reference to './node\_modules/@internal/design-tokens'. The error is cryptic. You dig through GitHub issues and find that when TypeScript generates the declaration file, it tries to serialize the inferred return type. Because the return type structurally depends on a type from an internal package that isn't explicitly imported in this file, it can't write a portable reference to it. The correct fix is to be explicit. You define an interface 'Theme' that only uses public types, and annotate the function: 'export function createTheme\(tokens: TokenInput\): Theme \{ ... \}'. The error disappears because the declaration emit now references the local 'Theme' interface, which is fully under your control and portable.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T19:07:56.585899+00:00— report_created — created