TypeScript is a programming language that adds static types to JavaScript so errors can be found earlier. This cheat sheet helps students remember common syntax for declaring variables, functions, objects, arrays, classes, and reusable types. It is useful when writing browser code, Node.js programs, or larger projects where clear structure matters.
Grades 9-12 students can use it as a quick reference while learning modern web development.
Key Facts
- Basic type annotations use the pattern let name: type = value, such as let score: number = 95.
- Common primitive types include string, number, boolean, null, undefined, symbol, and bigint.
- Arrays can be typed as number[] or Array<number>, so let scores: number[] = [90, 85, 100] stores only numbers.
- Union types allow more than one possible type, such as let id: string | number = 42.
- Functions can type parameters and return values, such as function add(a: number, b: number): number { return a + b; }.
- Interfaces describe object shapes, such as interface User { name: string; age: number; }.
- Optional properties use ?, such as interface User { name: string; age?: number; }.
- Generics use type variables for reusable code, such as function first<T>(items: T[]): T { return items[0]; }.
Vocabulary
- Type Annotation
- A type annotation is a label that tells TypeScript what kind of value a variable, parameter, or return value should have.
- Interface
- An interface defines the expected property names and types for an object.
- Union Type
- A union type allows a value to be one of several listed types.
- Generic
- A generic is a reusable type placeholder that lets code work with different data types safely.
- Type Inference
- Type inference is TypeScript's ability to figure out a value's type from the code without an explicit annotation.
- Compile-Time Error
- A compile-time error is a problem TypeScript reports before the program runs.
Common Mistakes to Avoid
- Using any too often is a mistake because it turns off TypeScript's type checking for that value and hides possible errors.
- Forgetting to type function return values can be a mistake because the function may return an unexpected value without being noticed early.
- Confusing optional properties with required properties is a mistake because age?: number means the property may be missing, not just set to zero or null.
- Assigning the wrong type to a variable is a mistake because let count: number = '5' stores a string where a number is required.
- Using a union type without narrowing is a mistake because TypeScript needs checks like typeof value === 'string' before string-only methods can be used safely.
Practice Questions
- 1 Write a TypeScript variable declaration for a studentName that must be a string and stores the value 'Maya'.
- 2 Write a TypeScript function named multiply that takes two number parameters and returns a number.
- 3 Create an interface named Book with a required title string, a required pages number, and an optional author string.
- 4 Explain why TypeScript can prevent some bugs before a program runs, even though the final code is still JavaScript.