Why this matters
Every web UI runs JavaScript, and most serious frontends are written in TypeScript. AI products are no exception: the chat interface, the streaming text, the citations panel and the approve-or-reject buttons are all TypeScript. Many teams also run Node backends.
Core ideas
- Single-threaded event loop: JavaScript runs one thing at a time; I/O happens in the background and its callbacks queue up. Long synchronous work freezes the page.
- Promises and
async/await: a promise is a value that will exist later.awaitpauses the function, not the whole program. - Modules:
import/export(ES modules).package.jsonlists dependencies;npmorpnpminstalls them; the lockfile pins exact versions. constandlet, nevervar. Strict equality===.- TypeScript adds static types that disappear at runtime. It catches mistakes at compile time and documents intent.
TypeScript you'll use every day
// Object shapes
interface Message { id: string; role: "user" | "assistant"; text: string }
// Discriminated unions: model every state explicitly
type ChatState =
| { status: "idle" }
| { status: "streaming"; partial: string }
| { status: "done"; message: Message }
| { status: "error"; error: string };
function render(s: ChatState): string {
switch (s.status) { // TypeScript narrows the type in each branch
case "idle": return "";
case "streaming": return s.partial + "▍";
case "done": return s.message.text;
case "error": return `Something went wrong: ${s.error}`;
}
}
// Generics
async function getJson<T>(url: string): Promise<T> {
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return (await res.json()) as T; // a cast, not a check: validate untrusted data (e.g. with zod)
}
Turn on "strict": true in tsconfig.json and keep it on.
Why this way
- Discriminated unions over boolean flags:
isLoading,isErroranddataas three separate fields can contradict each other; a union can't. This matters a lot for AI UIs with many states (4.5). - Types are not validation:
as Ttrusts the server. Data from the network should be parsed with a schema library (zod, valibot) when correctness matters, just as Pydantic does in Python. - TypeScript on the backend (Node, Bun, Deno) is a fine choice when the team is frontend-heavy; Python wins when you need the AI and data ecosystem.
Common mistakes
- Forgetting
await, so you get aPromisewhere you expected a value. anyeverywhere, which turns TypeScript off silently.- Mutating React state directly instead of creating new objects.
- Floating promises with no error handling (unhandled rejections).
Interview follow-ups
- "
interfaceortype?" Both describe shapes; interfaces can be extended and merged, types can express unions and mapped types. Pick one convention and stay consistent. - "What does
unknowngive you overany?"unknownforces you to narrow (or validate) before use;anyturns checking off. - "Microtasks vs macrotasks?" Promise callbacks (microtasks) run before the next timer or I/O callback (macrotasks), which explains surprising ordering in async code.
Check yourself
Why does a long `for` loop freeze a web page?
JavaScript in the page runs on one thread with the rendering work. While your loop runs, the event loop can't process clicks, paints or network callbacks. Break the work up, move it to a Web Worker, or do it on the server.
Does `res.json() as User` guarantee you have a valid `User`?
No. It's a compile-time assertion with no runtime check. Validate with a schema library if the data's shape matters.
Build it
Write a typed ChatState reducer in TypeScript with the four states above and unit tests (Vitest) for each transition, with strict mode on.
What comes next
1.6 Data structures and algorithms: what you need for coding interviews and for writing code that doesn't fall over at scale.