Report #72002
[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
Explicitly annotate the return type of the variable or function with a public-facing interface that does not leak the internal type, or export the referenced internal type \`Y\` from the package's public API \(even if just as a type export\). For example, change \`const myService = createService\(\)\` to \`const myService: ServiceApi = createService\(\)\` where \`ServiceApi\` is a manually defined interface. The root cause is that during declaration file \(\`.d.ts\`\) generation, TypeScript must be able to reference all types used in public APIs by name. If a type is inferred from an expression that references an unexported internal type, TypeScript cannot write a portable declaration file because the internal type path would not be accessible to consumers.
Journey Context:
A developer is building a library that exports a factory function. Inside \`src/service.ts\`, they import an internal config type \`InternalConfig\` from \`./internal/config\`. The factory function returns an object whose methods reference \`InternalConfig\` in their implementation \(e.g., a method \`reconfigure\(config: InternalConfig\)\`\). In \`src/index.ts\`, they export the factory: \`export const createService = \(\) => \{ ... \}\`. When they run \`tsc --declaration\` to generate \`.d.ts\` files, TypeScript throws TS2742: "The inferred type of 'createService' cannot be named without a reference to './internal/config'. This is likely not portable. A type annotation is necessary." The developer is confused because they didn't explicitly type the return value; TypeScript inferred it. They try exporting \`InternalConfig\` from the index, which fixes the error but leaks internal implementation details. They realize they should explicitly type the return value of \`createService\` using a public interface \`ServiceApi\` that uses \`Omit\` or picks only the public methods, breaking the dependency on \`InternalConfig\` in the public signature. They add the explicit return type annotation, and the declaration files emit successfully.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T03:26:28.065764+00:00— report_created — created