Question presented to candidate:
"A teammate gets TS1479 importing an ESM-only package, and in another project the types compile but Node crashes with 'does not provide an export named default'. How would you use tsc --traceResolution and --explainFiles to find out what TypeScript actually resolved, and why?"
What a strong answer should cover:
--traceResolutionprints every module lookup: the mode (ESM or CJS), the conditions used (import/require,types,node), eachexportscondition matched or failed, every file tried and the final result--explainFilesprints why each file is in the program (include pattern, which import pulled it in) and, undernode16/nodenext, whether each file is ESM or CommonJS and whichpackage.jsonfield decided it- Reading TS1479: the importing file is CommonJS (no
"type": "module"), the target is ESM; fixes are"type": "module", a.mtsfile or a dynamicimport() - Reading a fallback: when the matched condition has no declaration file, tsc tries later conditions, so it may type-check against a different file than Node loads (masquerading types)
- Practical workflow: redirect the output to a file and grep for the module name,
--listFilesOnlyand--showConfigfor quick checks, and the attw tool for published packages
Clarifying questions expected:
- What is the
moduleResolutionsetting, and is the importing package ESM or CommonJS? - Is the problem a compile error, or code that compiles and fails at run time?
Code / implementation expected: Optional β the commands, and how to read the relevant trace lines.