Edit the TypeScript example below and click Run. Use this to quickly test typed logic and UI behavior.
TypeScript tutorial
Learn TypeScript tutorial with a small example you can edit and run.
npm i -g typescript
npx tsc --init
npx tsc
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const msg: string = "Welcome to TypeScript"; | Declares a variable/constant (with types). |
console.log(msg); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TypeScript tutorialTS HOME
Learn TS HOME with a small example you can edit and run.
npm i -g typescript
npx tsc --init
npx tsc
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const msg: string = "Welcome to TypeScript"; | Declares a variable/constant (with types). |
console.log(msg); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS HOMETS Introduction
Learn TS Introduction with a small example you can edit and run.
npm i -g typescript
npx tsc --init
npx tsc
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const msg: string = "Welcome to TypeScript"; | Declares a variable/constant (with types). |
console.log(msg); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS IntroductionTS Get Started
Learn TS Get Started with a small example you can edit and run.
npm i -g typescript
npx tsc --init
npx tsc
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const msg: string = "Welcome to TypeScript"; | Declares a variable/constant (with types). |
console.log(msg); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Get StartedTS Simple Types
Learn TS Simple Types with a small example you can edit and run.
let n: number = 7;
let s: string = "hi";
let ok: boolean = true;
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const name: string = "Mana"; | Declares a variable/constant (with types). |
const score: number = 95; | Declares a variable/constant (with types). |
const ok: boolean = score >= 75; | Declares a variable/constant (with types). |
console.log(` passed? `); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Simple TypesTS Explicit & Inference
Learn TS Explicit & Inference with a small example you can edit and run.
let a = 1; // inferred number
let b: number = 2; // explicit
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
let a = 7; // inferred number | Declares a variable/constant (with types). |
let b: number = 3; // explicit | Declares a variable/constant (with types). |
const sum = a + b; | Declares a variable/constant (with types). |
console.log(sum); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Explicit & InferenceTS Special Types
Learn TS Special Types with a small example you can edit and run.
let a: any = 1;
let u: unknown = "x";
function fail(): never { throw new Error("x") }
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
let a: any = 1; | Declares a variable/constant (with types). |
let u: unknown = "7"; | Declares a variable/constant (with types). |
function toNumber(x: unknown): number { | Declares a function. |
if (typeof x === "string") return Number(x); | TypeScript line. |
if (typeof x === "number") return x; | TypeScript line. |
return 0; | TypeScript line. |
} | TypeScript line. |
console.log(toNumber(u)); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Special TypesTS Arrays
Learn TS Arrays with a small example you can edit and run.
let xs: number[] = [1, 2, 3];
let ys: Array<string> = ["a", "b"];
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const nums: number[] = [1, 2, 3, 4]; | Declares a variable/constant (with types). |
const sum = nums.reduce((a, b) => a + b, 0); | Declares a function. |
console.log(sum); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS ArraysTS Tuples
Learn TS Tuples with a small example you can edit and run.
let t: [number, string] = [1, "a"];
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const t: [number, string] = [7, "hi"]; | Declares a variable/constant (with types). |
console.log(` `); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS TuplesTS Object Types
Learn TS Object Types with a small example you can edit and run.
type User = { id: number; name: string };
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
type User = { id: number; name: string }; | Declares a type alias. |
const u: User = { id: 1, name: "Mana" }; | Declares a variable/constant (with types). |
console.log(u.name); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Object TypesTS Enums
Learn TS Enums with a small example you can edit and run.
enum Status { Active, Inactive }
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
enum Status { Active, Inactive } | Declares an enum. |
const s: Status = Status.Active; | Declares a variable/constant (with types). |
console.log(s === Status.Active ? "Active" : "Inactive"); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS EnumsTS Aliases & Interfaces
Learn TS Aliases & Interfaces with a small example you can edit and run.
type Id = string | number;
interface User { name: string }
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
type Id = string | number; | Declares a type alias. |
interface User { id: Id; name: string } | Declares an interface. |
const u: User = { id: 1, name: "Mana" }; | Declares a variable/constant (with types). |
console.log(u.id); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Aliases & InterfacesTS Union Types
Learn TS Union Types with a small example you can edit and run.
let v: string | number = 1;
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
function label(x: string | number): string { | Declares a function. |
return typeof x === "number" ? `n=` : `s=`; | TypeScript line. |
} | TypeScript line. |
console.log(label(7)); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Union TypesTS Functions
Learn TS Functions with a small example you can edit and run.
function add(a: number, b: number): number { return a + b }
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
function add(a: number, b: number): number { return a + b } | Declares a function. |
console.log(add(4, 6)); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS FunctionsTS Casting
Learn TS Casting with a small example you can edit and run.
const n = ("7" as unknown as number);
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const raw: unknown = "42"; | Declares a variable/constant (with types). |
const n = Number(raw as string); | Declares a variable/constant (with types). |
console.log(n + 1); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS CastingTS Classes
Learn TS Classes with a small example you can edit and run.
class User { constructor(public name: string) {} }
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
class Counter { | Declares a class. |
private n = 0; | TypeScript line. |
inc() { this.n++; } | TypeScript line. |
value() { return this.n; } | TypeScript line. |
} | TypeScript line. |
const c = new Counter(); | Declares a variable/constant (with types). |
c.inc(); c.inc(); | TypeScript line. |
console.log(c.value()); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS ClassesTS Basic Generics
Learn TS Basic Generics with a small example you can edit and run.
function id<T>(x: T): T { return x }
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
function first<T>(xs: T[]): T { return xs[0] } | Declares a function. |
console.log(first(["Mana", "Coding"])); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Basic GenericsTS Utility Types
Learn TS Utility Types with a small example you can edit and run.
type P = Partial<{ a: number; b: number }>;
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
type User = { id: number; name: string; active: boolean }; | Declares a type alias. |
type Preview = Pick<User, "id" | "name">; | Declares a type alias. |
const p: Preview = { id: 1, name: "Mana" }; | Declares a variable/constant (with types). |
console.log(p.name); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Utility TypesTS Keyof
Learn TS Keyof with a small example you can edit and run.
type Keys = keyof { a: 1; b: 2 };
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
type User = { id: number; name: string }; | Declares a type alias. |
type K = keyof User; | Declares a type alias. |
const key: K = "name"; | Declares a variable/constant (with types). |
console.log(key); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS KeyofTS Null
Learn TS Null with a small example you can edit and run.
let x: string | null = null;
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
function show(x: string | null): string { | Declares a function. |
return x ?? "(empty)"; | TypeScript line. |
} | TypeScript line. |
console.log(show(null)); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS NullTS Definitely Typed
Learn TS Definitely Typed with a small example you can edit and run.
npm i -D @types/node
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
console.log("Types ready"); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Definitely TypedTS 5 Updates
Learn TS 5 Updates with a small example you can edit and run.
const p = { x: 1 } satisfies { x: number };
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const input = { id: 1, name: "Mana" } satisfies { id: number; name: string }; | Declares a variable/constant (with types). |
console.log(input.name); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS 5 UpdatesTS Configuration
Learn TS Configuration with a small example you can edit and run.
{
"compilerOptions": { "strict": true }
}
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
console.log("tsconfig set"); | Prints output to the console. |
- 1Tooling and config decide how your code builds, checks, and ships.
- 1Loose tsconfig settings causing runtime surprises.
- 1Use strict settings and keep TS/ESLint/Prettier aligned.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS ConfigurationTS with Node.js
Learn TS with Node.js with a small example you can edit and run.
import fs from "node:fs";
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const env = "development"; | Declares a variable/constant (with types). |
console.log(env); | Prints output to the console. |
- 1Tooling and config decide how your code builds, checks, and ships.
- 1Loose tsconfig settings causing runtime surprises.
- 1Use strict settings and keep TS/ESLint/Prettier aligned.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS with Node.jsTS with React
Learn TS with React with a small example you can edit and run.
type Props = { title: string };
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const title: string = "Hello"; | Declares a variable/constant (with types). |
console.log(title); | Prints output to the console. |
- 1Tooling and config decide how your code builds, checks, and ships.
- 1Loose tsconfig settings causing runtime surprises.
- 1Use strict settings and keep TS/ESLint/Prettier aligned.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS with ReactTS Tooling
Learn TS Tooling with a small example you can edit and run.
tsc --watch
eslint .
prettier --write .
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
console.log("Tooling OK"); | Prints output to the console. |
- 1Tooling and config decide how your code builds, checks, and ships.
- 1Loose tsconfig settings causing runtime surprises.
- 1Use strict settings and keep TS/ESLint/Prettier aligned.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS ToolingTS Advanced Types
Learn TS Advanced Types with a small example you can edit and run.
type User = { id: number; name: string };
type Id = User["id"];
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
type ApiUser = { id: number; name: string; active: boolean }; | Declares a type alias. |
type ReadonlyUser = { readonly [K in keyof ApiUser]: ApiUser[K] }; | Declares a type alias. |
type Id = ApiUser["id"]; | Declares a type alias. |
const id: Id = 1; | Declares a variable/constant (with types). |
console.log(id); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Advanced TypesTS Type Guards
Learn TS Type Guards with a small example you can edit and run.
function isString(x: unknown): x is string { return typeof x === "string" }
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
function isString(x: unknown): x is string { | Declares a function. |
return typeof x === "string"; | TypeScript line. |
} | TypeScript line. |
const v: unknown = "Mana"; | Declares a variable/constant (with types). |
console.log(isString(v) ? v.toUpperCase() : "not a string"); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Type GuardsTS Conditional Types
Learn TS Conditional Types with a small example you can edit and run.
type IsString<T> = T extends string ? true : false;
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
type IsString<T> = T extends string ? true : false; | Declares a type alias. |
type A = IsString<string>; | Declares a type alias. |
type B = IsString<number>; | Declares a type alias. |
console.log("Conditional types ready"); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Conditional TypesTS Mapped Types
Learn TS Mapped Types with a small example you can edit and run.
type RO<T> = { readonly [K in keyof T]: T[K] };
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
type User = { id: number; name: string }; | Declares a type alias. |
type RO<T> = { readonly [K in keyof T]: T[K] }; | Declares a type alias. |
const u: RO<User> = { id: 1, name: "Mana" }; | Declares a variable/constant (with types). |
console.log(u.name); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Mapped TypesTS Type Inference
Learn TS Type Inference with a small example you can edit and run.
let a = 1; // inferred number
let b: number = 2; // explicit
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
let a = 7; // inferred number | Declares a variable/constant (with types). |
let b: number = 3; // explicit | Declares a variable/constant (with types). |
const sum = a + b; | Declares a variable/constant (with types). |
console.log(sum); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Type InferenceTS Literal Types
Learn TS Literal Types with a small example you can edit and run.
type Dir = "left" | "right";
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
type Dir = "left" | "right"; | Declares a type alias. |
function move(d: Dir){ return d === "left" ? "L" : "R" } | Declares a function. |
console.log(move("right")); | Prints output to the console. |
- 1Types protect APIs, forms, and refactors by making contracts explicit.
- 1Overcomplicating types before the domain is clear.
- 1Start with readable types; refactor to advanced types when needed.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Literal TypesTS Namespaces
Learn TS Namespaces with a small example you can edit and run.
namespace App { export const v = 1 }
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
namespace App { | Groups declarations in a namespace. |
export const version = 1; | TypeScript line. |
} | TypeScript line. |
console.log(App.version); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS NamespacesTS Index Signatures
Learn TS Index Signatures with a small example you can edit and run.
type Bag = { [k: string]: number };
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
type Bag = { [k: string]: number }; | Declares a type alias. |
const b: Bag = { sql: 2, go: 1 }; | Declares a variable/constant (with types). |
console.log(b["sql"]); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Index SignaturesTS Declaration Merging
Learn TS Declaration Merging with a small example you can edit and run.
interface User { name: string }
interface User { id: number }
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
console.log("Merging OK"); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Declaration MergingTS Async Programming
Learn TS Async Programming with a small example you can edit and run.
async function f(): Promise<number> { return 1 }
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
async function getUser(): Promise<{ id: number; name: string }> { | Declares async code returning a Promise. |
return { id: 1, name: "Mana" }; | TypeScript line. |
} | TypeScript line. |
getUser().then(u => console.log(u.name)); | Declares a function. |
- 1Async typing prevents Promise misuse and makes APIs safer.
- 1Catching errors as any instead of unknown.
- 1Prefer unknown in catch and narrow safely.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Async ProgrammingTS Decorators
Learn TS Decorators with a small example you can edit and run.
// @decorator requires "experimentalDecorators": true
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
console.log("Decorators enabled"); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS DecoratorsTS in JS Projects
Learn TS in JS Projects with a small example you can edit and run.
// @ts-check
// JSDoc types in .js files
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
console.log("JS project typing"); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS in JS ProjectsTS Migration
Learn TS Migration with a small example you can edit and run.
// rename .js -> .ts
// fix errors
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
console.log("Migration ready"); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS MigrationTS Error Handling
Learn TS Error Handling with a small example you can edit and run.
try { throw new Error("x") } catch (e: unknown) {}
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
function safeJson(input: string): string { | Declares a function. |
try { | TypeScript line. |
const v: unknown = JSON.parse(input); | Declares a variable/constant (with types). |
return typeof v === "object" && v ? "ok" : "bad"; | TypeScript line. |
} catch (e: unknown) { | TypeScript line. |
return "error"; | TypeScript line. |
} | TypeScript line. |
} | TypeScript line. |
console.log(safeJson("not json")); | Prints output to the console. |
- 1Async typing prevents Promise misuse and makes APIs safer.
- 1Catching errors as any instead of unknown.
- 1Prefer unknown in catch and narrow safely.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Error HandlingTS Best Practices
Learn TS Best Practices with a small example you can edit and run.
"strict": true
"noUncheckedIndexedAccess": true
Edit the TypeScript code below and click Run. The output panel shows the extracted Expected Output notes (or simple logs).
// Expected Output: line so the output panel has something to show.| Line | Meaning |
|---|---|
const items: string[] = ["Go", "TS", "Rust"]; | Declares a variable/constant (with types). |
console.log(items.join(",")); | Prints output to the console. |
- 1TypeScript is widely used for web apps, Node.js services, and typed UI components.
- 1Using any everywhere and losing type safety.
- 1Turn on strict mode and type your boundaries first.
- 1Edit the example and rerun to learn faster.
- 2Prefer clarity first; optimize later.
- 1Change one value and predict the output.
- 2Add one edge case and handle it.
- 3Explain the rule in 1 sentence.
Keywords (topic intent):
TypeScript JavaScript TS Best Practices