Report #22296
[bug\_fix] TS2742: The inferred type of 'X' cannot be named without a reference to 'Y'. This is likely not portable. A type annotation is necessary.
Add an explicit return type annotation to the exported function/class, explicitly importing any complex dependent types from the referenced package
Journey Context:
Developer is building a library with 'declaration': true in tsconfig.json to generate .d.ts files. They export a factory function that internally uses types from a dependency \(e.g., axios or zod\) but doesn't explicitly reference those types in the export signature. TypeScript tries to infer the return type, which includes the internal dependency's types. When emitting the declaration file, TypeScript errors with 'The inferred type of 'createApi' cannot be named without a reference to 'axios'. This is likely not portable.' The developer initially tries exporting the internal type, but the error persists because the inference creates a complex nested type. The solution is to manually annotate the return type: 'import \{ AxiosInstance \} from 'axios'; export function createApi\(\): \{ client: AxiosInstance \} \{ ... \}'. By explicitly typing the return value and importing AxiosInstance, the generated .d.ts file now has a clear, portable reference that doesn't depend on the internal inference structure.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T15:50:03.313316+00:00— report_created — created