Learn TypeScript

A shared folder with AI prompts and code snippets

From workspace: FreeCodeCamp

Team: Main

Total snippets: 5

FreeCodeCamp

Learn TypeScript

5 snippets

Convert JavaScript File to TypeScript

Rename .js to .ts and run the compiler to discover type issues.

mv app.js app.ts tsc --strict

Enable Strict Mode in tsconfig.json

Use strict mode to enable all core type-safety features.

{ "compilerOptions": { "strict": true } }

Compile-Time Error Example

Demonstrates how TypeScript catches type mismatch errors before runtime.

function multiply(a: string, b: string): number { return a * b; // ❌ Error: Operator '*' cannot be applied to type 'string' }

Function with Type Annotations

Define a function with typed parameters and a typed return value.

function multiply(a: number, b: number): number { return a * b; }

What Is TypeScript?

TypeScript is a superset of JavaScript that adds static type safety.

// TypeScript example vs JavaScript: let count: number = 5; // TS: type-safe let name: string = "John"; // JS would allow anything, TS enforces string