Report #12583
[bug\_fix] Module '"/path/to/node\_modules/@types/react/index"' can only be default-imported using the 'esModuleInterop' flag. \(TS1259\)
Enable \`compilerOptions.esModuleInterop\` and \`compilerOptions.allowSyntheticDefaultImports\` \(the former implies the latter\) in \`tsconfig.json\`. This allows default importing \(\`import React from 'react'\`\) from CommonJS modules that export \`module.exports = ...\`, matching the interoperability behavior of Babel and modern Node.js ESM loaders. The root cause is that without this flag, TypeScript treats CommonJS \`module.exports\` as a namespace \(\`\* as React\`\) rather than a default export, strictly enforcing ES module semantics where a 'default' import must correspond to an \`export default\` statement in the source.
Journey Context:
Developer starts a new TypeScript project without \`strict\` or \`esModuleInterop\` enabled \(e.g., using an older \`tsc --init\` or a minimalist config\). They install \`react\` and \`@types/react\`. They write \`import React from 'react';\`. TypeScript immediately flags it with TS1259, suggesting they can only default-import it with the \`esModuleInterop\` flag. Confused, they change the import to \`import \* as React from 'react';\`, which satisfies the compiler. However, later they use a library that expects a default export, or they see runtime errors in their bundler \(Webpack/Vite\) because the interop between the namespace import and the actual CommonJS \`module.exports\` object is messy. They research the error code TS1259 and discover that \`esModuleInterop\` is a flag designed specifically to allow the ergonomic \`import Foo from 'foo'\` syntax for CommonJS modules, emitting helper code \(or assuming the runtime/bundler handles it\) to access the \`.default\` property correctly. They enable the flag, revert to \`import React from 'react'\`, and everything works consistently across type-checking and runtime.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T16:20:39.809662+00:00— report_created — created