Question presented to candidate:
"You publish a package that works for some users and breaks for others: ESM users get a type error on a default import that runs fine, CommonJS users get told to use a dynamic import. What does arethetypeswrong check, and how would you fix the package?"
What a strong answer should cover:
- What attw does: packs the package and resolves every
exportsentrypoint the way TypeScript does undernode10,node16(from CJS and from ESM) andbundler, then compares the declaration file it finds with the JavaScript file Node would load - The core rule: under
node16/nodenexta.d.tsfile's module format comes from the nearestpackage.json"type"(.d.mtsis always ESM,.d.ctsalways CJS), so one sharedindex.d.tscannot describe both an.mjsand a.cjsfile - The main problem kinds by name: Masquerading as CJS / as ESM, Incorrect default export (
export defaulttyped formodule.exports =), CJS default export, Used fallback condition, Resolution failed / No types, Named exports - The fix: one declaration file per format (
.d.mtsnext to.mjs,.d.ctsnext to.cjs), nestedimport/requireconditions with"types"first in each, andexport =for a CommonJS file that assignsmodule.exports - How it is used:
attw --pack .(or--profile node16/esm-only) in CI before publishing, because a wrong.d.tsis trusted by TypeScript and only fails for some consumers
Clarifying questions expected:
- Does the package need to support CommonJS consumers at all, or can it be ESM-only?
- Which module resolution settings do the consumers use (
bundler,nodenext, oldnode10)?
Code / implementation expected: Optional โ a broken and a fixed package.json exports map, and why a default import type-checks wrongly.