TypeScript Type System
Also Known As
TypeScript
TS types
type annotations
TL;DR
TypeScript adds static types to JavaScript — catching type errors at compile time rather than runtime, while remaining a superset that compiles to plain JavaScript.
Explanation
TypeScript's type system is structural (duck typing) not nominal — if an object has the required properties, it satisfies the type regardless of its declared class. Types include primitives, unions (string|number), intersections (A&B), literal types ('GET'|'POST'), tuples, enums, and generics. The any type disables checking; unknown is the safe alternative. Type inference reduces annotation noise — TypeScript infers types from assignments. strict mode (strictNullChecks, noImplicitAny) catches the most bugs.
Common Misconception
✗ TypeScript types exist at runtime — TypeScript is erased to JavaScript at compile time; there are no runtime type checks unless you add them explicitly (Zod, class-validator).
Why It Matters
TypeScript catches entire categories of bugs before runtime — passing the wrong argument type, accessing undefined properties, and incorrect return types are all compile-time errors, not production bugs.
Common Mistakes
- Using any instead of unknown — any disables all type checking; unknown forces you to narrow the type before using it.
- Not enabling strict mode — without strictNullChecks, null and undefined assignment errors are invisible.
- Type assertions (as Type) without validation — asserting a type without checking at runtime creates false confidence.
- Over-annotating when inference works — TypeScript infers const name = 'Alice' as string; no annotation needed.
Code Examples
✗ Vulnerable
// any disables type checking:
function process(data: any) {
return data.user.name.toUpperCase(); // No error if data is null
}
// Type assertion without validation:
const user = response.data as User; // Unsafe if response is actually an error object
console.log(user.email.toLowerCase()); // Runtime crash if email is undefined
✓ Fixed
// unknown forces narrowing before use:
function process(data: unknown): string {
if (typeof data !== 'object' || data === null) throw new Error('Invalid data');
if (!('user' in data)) throw new Error('Missing user');
const user = (data as { user: { name: string } }).user;
return user.name.toUpperCase();
}
// Runtime validation with Zod:
const UserSchema = z.object({ email: z.string().email() });
const user = UserSchema.parse(response.data); // Throws if invalid
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
28 Apr 2026
Views
40
AI edit
PF Media Bot
Claude Opus 4.5 on refs · 27 Apr 2026
Edits history 1 edit
- refs PF Media Bot Claude Opus 4.5 · 27 Apr 2026
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Perplexity 10
Amazonbot 8
Unknown AI 3
Ahrefs 2
Google 2
SEMrush 2
Bing 1
Also referenced
How they use it
crawler 26
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Prefer type aliases for unions and intersections, interfaces for object shapes that will be extended — the practical difference is minimal but type aliases cannot be augmented after declaration
📦 Applies To
typescript 2.0
web
cli
🔍 Detection Hints
Mixing interface and type inconsistently; using any when unknown would be safer; no strict null checks causing runtime null errors
Auto-detectable:
✓ Yes
typescript
eslint
tsc
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✗ Manual fix
Fix: Medium
Context: File