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.
Understanding TypeScript Quick Reference
A TypeScript program is checked before it runs. The checker compares the values a piece of code might receive with the values that code claims it can handle. It then reports mismatches during development.
After checking, TypeScript is converted into ordinary JavaScript. Browsers and Node.js run that JavaScript, not the type information.
This matters because a type can prevent a mistake while writing code, but it cannot validate data arriving at runtime. A form field, a saved file, or a network response can still contain unexpected data.
Type inference reduces the amount of typing needed. When a variable starts with a clear value, TypeScript often infers its type automatically. Good inference keeps code readable, but explicit types are useful at important boundaries.
Function parameters, returned values, shared objects, and data from an outside source deserve clear descriptions. A helpful habit is to ask what values are truly allowed, rather than choosing a broad type just to silence an error. Using any removes many useful checks, so it should be rare in student projects.
Safe checking becomes especially important with values that may be missing. A property marked optional might be undefined, even if it exists in most examples. Code must check before using it.
TypeScript narrows a union when a condition proves more about a value. For example, checking whether an identifier is a string lets later code use string methods safely inside that branch. Checks such as typeof, Array.isArray, and comparisons with null help the compiler follow the same reasoning a careful programmer should use.
Interfaces are useful for describing the data passed between parts of a program. A game player object might require a name and score, while an online shop item might require a price and product code. TypeScript uses structural typing.
This means an object is accepted when it has the required properties with compatible types, even if it was not created from a particular class. Type aliases can describe similar shapes, plus unions and more complex combinations. Classes are best when an object needs stored state and methods that work on that state, such as a timer that starts, stops, and reports elapsed time.
Generics preserve type information in reusable code. A generic list function can work with numbers, names, or custom objects without forgetting what kind of item it received. This is safer than returning a vague value that must be guessed later.
Generic constraints can require a minimum feature, such as an object with a name property. Students often meet these ideas when using arrays, promises, API data, and library functions.
Pay attention to error messages rather than treating them as obstacles. They often point to an unclear assumption about missing data, a wrong property name, or a value whose type has not been checked yet.