Skip to the solution
HardFrontend13 min read

What is isolatedDeclarations and how does it enable single-file transpilation for declaration emit?

286 views

The problem

Question presented to candidate: "Transpiling TypeScript to JavaScript can be done one file at a time by fast tools, but generating .d.ts files has always needed tsc. Why is that, and what does the isolatedDeclarations option change?"

What a strong answer should cover:

  • Why declaration emit needs a type checker: an unannotated export (a function return type, a variable set from a call) gets its type from inference, which can depend on other files and on the whole program
  • What isolatedDeclarations does (TypeScript 5.5+): it reports an error wherever an export's type cannot be written down from that file's syntax alone, so any tool can produce the .d.ts file by copying annotations, with no checker
  • What still needs no annotation ("trivially inferable"): literals, object literals of literals, as const arrays, type assertions; what does (calls, new, spreads, plain arrays, unannotated returns, parameters with non-literal defaults) and the TS90xx error family
  • What it does not do: it changes no emit by itself, needs declaration or composite, and tsc writes no .d.ts while those errors exist
  • The payoff: per-file, parallel declaration emit (ts.transpileDeclaration, oxc, bundler plugins) and starting downstream type checks before upstream projects finish

Code / implementation expected: Optional β€” an exported function that fails under the flag, the error, and the annotated version.

  • isolatedDeclarations
  • declarations

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

13 min read Β· 11 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptWhy declaration emit normally needs other files
TypeScriptA toy model of the trivially inferable rule

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 79 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What are conditional types?

Continue