About the TypeScript online compiler
TypeScript is JavaScript with a type layer on top. This online TypeScript compiler type-checks your snippet and then runs it, so you see both compile-time errors and runtime output in one place — the fastest way to learn how interfaces, generics and unions behave in practice.
Start with typing function parameters and return values, then move on to interfaces versus type aliases, union and literal types, optional properties, and generics. Every mistake you make here shows up as a red error before the program even runs, which is exactly the habit TypeScript is meant to build.
- →Learning generics by writing a typed identity or wrapper function
- →Modelling API responses with interfaces before writing the fetch
- →Testing whether a union narrows correctly inside an if block
- →Practising utility types like Partial, Pick and Record
TypeScript sample programs you can run right now
Copy any snippet into the editor above and press Run. Each one is short on purpose — retype it from memory afterwards.
Interfaces and narrowing
interface User { name: string; age: number; admin?: boolean }
function describe(u: User): string {
return u.admin ? `${u.name} (admin)` : `${u.name}, ${u.age}`;
}
console.log(describe({ name: "Aarav", age: 21 }));
console.log(describe({ name: "Priya", age: 24, admin: true }));
The ? marks admin as optional, so the first call compiles even without it.
A generic function
function first<T>(items: T[]): T | undefined {
return items[0];
}
console.log(first([10, 20, 30]));
console.log(first(["a", "b"]));
T is inferred from the argument, so first([10,20]) returns number | undefined without any annotation at the call site.
Common TypeScript errors and how to fix them
Type 'string' is not assignable to type 'number'
Why: The value's type does not match the declared type.
Fix: Either change the annotation or convert the value with Number(x) / String(x).
Property 'x' does not exist on type '{}'
Why: TypeScript inferred an empty object type.
Fix: Declare an interface or annotate the variable so the property is known.
Object is possibly 'undefined'
Why: Strict null checks caught an access that could fail at runtime.
Fix: Use optional chaining (a?.b) or an explicit if (a) guard.
TypeScript syntax cheatsheet
| Concept | Syntax |
|---|
| Typed variable | let count: number = 0; |
| Interface | interface Point { x: number; y: number } |
| Union type | type Status = 'on' | 'off'; |
| Generic | function id<T>(v: T): T { return v } |
| Optional property | interface A { b?: string } |
| Type assertion | const el = v as string; |
TypeScript compiler FAQs
Is the TypeScript compiler here the real tsc?
Your code is compiled to JavaScript and executed, so syntax and type errors surface exactly as they would in a local project.
Can I import npm packages?
No — snippets run standalone with no package installation, so keep examples dependency free.
Keep going after the compiler
Running snippets builds speed; the 17-chapter course builds understanding. Work through the TypeScript chapters, take the quiz, then claim your certificate.
Other compilers: JavaScript compiler, Python 3 compiler, Java compiler, C compiler, C++ compiler, Go compiler, Rust compiler, HTML compiler, CSS compiler, SQLite compiler