TypeScript Cheatsheet
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. This cheatsheet covers TypeScript features, configurations, advanced typing concepts, and best practices.
1. Basic Types
let isDone: boolean = false;
let decimal: number = 6;
let color: string = "blue";
let list: number[] = [1, 2, 3];
let genericList: Array<number> = [1, 2, 3]; // alternative syntax
Tuple
Fixed-length arrays with known element types at specific positions.
let x: [string, number];
x = ["hello", 10]; // OK
// x = [10, "hello"]; // Error
Enum
A way of giving more friendly names to sets of numeric or string values.
enum Color {
Red = 1,
Green,
Blue,
}
let c: Color = Color.Green; // 2
// String enums
enum Direction {
Up = "UP",
Down = "DOWN",
}
Any, Unknown, Void, Null, and Undefined
any: Opt-out of type checking. Avoid using this.unknown: Safe counterpart ofany. You cannot perform arbitrary operations on anunknownwithout narrowing/type checking.void: Commonly used as the return type of functions that do not return a value.never: Represents types of values that never occur (e.g., a function that always throws an exception or never terminates).
let loose: any = 4;
loose.ifItExists(); // No compiler error
let strictlyUnknown: unknown = 4;
// strictlyUnknown.ifItExists(); // Error!
if (typeof strictlyUnknown === "function") {
strictlyUnknown(); // OK after type guard
}
2. Advanced Types
Union and Intersection Types
// Union: value can be one of several types
function printId(id: number | string) {
console.log("Your ID is: " + id);
}
// Intersection: combines multiple types into one
interface ErrorHandling {
success: boolean;
error?: { message: string };
}
interface ArtworksData {
artworks: { title: string }[];
}
type ArtworksResponse = ErrorHandling & ArtworksData;
Type Aliases vs Interfaces
- Interfaces are basically a way to describe data shapes (e.g., an object). They can be extended/merged.
- Type Aliases can describe any type including primitives, unions, and intersections. They cannot be merged.
// Interface extension
interface Animal {
name: string;
}
interface Bear extends Animal {
honey: boolean;
}
// Interface merging (declaration merging)
interface Window {
title: string;
}
interface Window {
customProp: string;
} // Now Window has both fields!
// Type Alias extension via intersections
type Point = {
x: number;
y: number;
};
type Point3D = Point & {
z: number;
};
Mapped Types, Conditional Types & Template Literal Types
Mapped types allow you to create new types based on old ones by mapping over keys. Conditional types choose types based on a relation test.
// Mapped Type
type OptionsFlags<Type> = {
[Property in keyof Type]: boolean;
};
type Features = { darkMode: () => void; newUser: boolean };
type FeatureOptions = OptionsFlags<Features>; // { darkMode: boolean; newUser: boolean }
// Conditional Type
type IsString<T> = T extends string ? true : false;
type A = IsString<string>; // true
type B = IsString<number>; // false
// 'infer' keyword inside conditional types
type GetReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type Num = GetReturnType<() => number>; // number
// Template Literal Type
type EmailLocaleIDs = "welcome_email" | "email_heading";
type FooterLocaleIDs = "footer_title" | "footer_send";
type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;
// "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_send_id"
3. Generics
Generics provide a way to make components work over a variety of types rather than a single one.
function identity<T>(arg: T): T {
return arg;
}
let output = identity<string>("myString");
// Generic Classes
class GenericNumber<T> {
zeroValue!: T;
add!: (x: T, y: T) => T;
}
// Generic Constraints
interface Lengthwise {
length: number;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
console.log(arg.length); // Now we know it has a .length property
return arg;
}
4. Utility Types
TypeScript provides several utility types to facilitate common type transformations.
| Utility Type | Description | Example |
|---|---|---|
Partial<T> |
Constructs a type with all properties of T set to optional. |
Partial<User> |
Required<T> |
Constructs a type with all properties of T set to required. |
Required<User> |
Readonly<T> |
Constructs a type with all properties of T set to readonly. |
Readonly<User> |
Record<K, T> |
Constructs an object type whose property keys are K and whose values are T. |
Record<string, PageInfo> |
Pick<T, K> |
Constructs a type by picking the set of properties K from T. |
Pick<User, "id" \| "name"> |
Omit<T, K> |
Constructs a type by omitting the set of properties K from T. |
Omit<User, "password"> |
Exclude<T, U> |
Excludes types from T that are assignable to U. |
Exclude<"a" \| "b", "a"> |
Extract<T, U> |
Extracts types from T that are assignable to U. |
Extract<"a" \| "b", "a"> |
ReturnType<T> |
Obtains the return type of a function type. | ReturnType<typeof fetchUser> |
5. Type Narrowing & Guards
TypeScript uses type narrowing to identify the most specific type possible inside conditionals.
Type Guard Comparisons
| Guard Type | Operator/Pattern | Targets | Runtime Check | Compile-time Narrowing |
|---|---|---|---|---|
| typeof | typeof x === "string" |
Primitives (string, number, boolean, symbol) |
Yes (standard JS) | Yes |
| instanceof | x instanceof Date |
Classes & constructor functions | Yes (prototype chain check) | Yes |
| in | "swim" in pet |
Object property existence | Yes | Yes |
| Custom Predicate | x is Fish |
Complex shapes / custom logic | Custom boolean function | Yes |
// typeof guard
function doSomething(x: number | string) {
if (typeof x === "number") {
return x.toFixed(2); // x is narrowed to number
}
return x.toUpperCase(); // x is narrowed to string
}
// instanceof guard
class Foo {
foo = 123;
}
class Bar {
bar = 456;
}
function doStuff(arg: Foo | Bar) {
if (arg instanceof Foo) {
console.log(arg.foo); // arg is narrowed to Foo
} else {
console.log(arg.bar); // arg is narrowed to Bar
}
}
// Custom Type Predicates (User-Defined Type Guards)
interface Fish { swim: () => void; }
interface Bird { fly: () => void; }
function isFish(pet: Fish | Bird): pet is Fish {
return (pet as Fish).swim !== undefined;
}
6. TSConfig Quick Reference (tsconfig.json)
{
"compilerOptions": {
"target": "es2022", /* Specify ECMAScript target version */
"module": "commonjs", /* Specify module code generation */
"lib": ["es2022", "dom"], /* Specify library files to be included in the compilation */
"strict": true, /* Enable all strict type-checking options */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules */
"skipLibCheck": true, /* Skip type checking of declaration files */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file */
"outDir": "./dist", /* Redirect output structure to the directory */
"rootDir": "./src" /* Specify the root directory of input files */
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
7. Common Gotchas
anyvsunknown: Avoidanywhenever possible. Useunknownif the type is truly not known beforehand, then narrow the type before working with it.- Non-Null Assertion Operator (
!): Writinguser!.nametells TS that you are 100% sureuseris not null. Use this with caution as it can hide runtime crashes. - Array Type Declarations:
const list = []will be inferred asany[]. Always supply a type signature likeconst list: string[] = []orconst list: Array<string> = []. - Structural Typing (Duck Typing): TypeScript type checking is based on the shape of values, not explicit declaration. If two objects have the same properties, they are considered to be of the same type.
Best Practices & Production Standards
- Enable Strict Mode: Always configure
strict: trueintsconfig.jsonto enforce null safety and strict type check constraints. - Avoid Type Assertions: Prefer type guards or narrow types instead of using
as MyTypeassertions which bypass compile-time safety checks. - Utilize Generics constraints: Write reusable, bounded generic functions using the
extendskeyword.
Common Mistakes & Antipatterns
- Abusing
any: Usinganyinstead ofunknownfor dynamic values, destroying type-safety and compile-time validation. - Mismatched Union Narrowing: Forgetting to write discriminating field properties on unified interface structures, resulting in runtime access failures.
- Over-annotating Types: Explicitly writing types for variables that the TypeScript compiler can infer automatically.
Troubleshooting & Debugging Guide
Common Compiler Errors & Solutions
- Property ‘X’ does not exist on type ‘Y’: Implement a custom type guard using
inortypeofcheck to narrow down union structures before accessing properties.// Wrong: function handle(input: { foo: string } | { bar: number }) { console.log(input.foo); // Error! } // Right: function handle(input: { foo: string } | { bar: number }) { if ("foo" in input) { console.log(input.foo); // Narrowed safely } } - Type instantiation is excessively deep: Refactor recursive interface declarations, or use simpler index lookup types.
- Object literal may only specify known properties: Avoid passing raw literal objects with extra attributes directly to strict interfaces. Use a temporary variable to allow duck typing.
interface User { name: string; } // Error: const user: User = { name: "Alice", age: 30 }; // Allowed (duck-typing through reference): const tmp = { name: "Alice", age: 30 }; const user2: User = tmp; // OK!
Core Interview Questions & Answers
- Q: What is the difference between
interfaceandtypealias in TypeScript?- A: Both can describe object shapes. Interfaces are open to extension via declaration merging and have faster compiler performance. Type aliases can define unions, primitives, tuples, and mapped types.
- Q: Explain the difference between
any,unknown, andnever.- A:
anydisables all type-checking.unknownrepresents any value but is type-safe; you must perform type narrowing/checking before calling methods on it.neverrepresents the type of values that never occur (e.g., functions that throw exceptions or infinite loops).
- A:
Technical Architecture Diagram
graph TD
TS[TypeScript Code] --> Parser[TS Compiler: AST Generation]
Parser --> TypeCheck[Type Checker Validation]
TypeCheck --> Emit[Emit Phase: Raw JS output]