22 cards · Public · Shuffle

TypeScript - Advanced Types & Modern Web Development

Master advanced TypeScript concepts and seamlessly integrate them into modern web frameworks like React, Angular, and Vue. This deck covers utility types, generics, decorators, and best practices for building robust, type-safe applications.

Cards in this deck

(22 cards)

Preview terms and definitions before starting your study session.

#1
Term
What is Partial<T> and when would you use it?
Definition
Partial<T> is a TypeScript utility type that constructs a type with all properties of T set to optional. It's useful when you want to create an object that might only have a subset of properties from an existing type, for example, when updating an entity.
typescript8 lines
interface User {
  id: number;
  name: string;
  email: string;
}
type PartialUser = Partial<User>;
// Equivalent to: { id?: number; name?: string; email?: string; }
const updateUser = (id: number, changes: PartialUser) => { /* ... */ };
#2
Term
Explain Readonly<T> and its primary use case.
Definition
Readonly<T> is a utility type that constructs a type with all properties of T set to readonly. This prevents modification of an object's properties once it has been assigned, enforcing immutability.
typescript8 lines
interface Point {
  x: number;
  y: number;
}
type ReadonlyPoint = Readonly<Point>;
// Equivalent to: { readonly x: number; readonly y: number; }
const origin: ReadonlyPoint = { x: 0, y: 0 };
// origin.x = 1; // Error: Cannot assign to 'x' because it is a read-only property.
#3
Term
Differentiate between Pick<T, K> and Omit<T, K>.
Definition
  • Pick<T, K> constructs a type by picking the set of properties K (a union of string literals) from type T.
  • Omit<T, K> constructs a type by picking all properties from T and then removing K.
Both are useful for creating new types based on existing ones with specific properties included or excluded.
typescript10 lines
interface Product {
  id: string;
  name: string;
  price: number;
  description: string;
}
type ProductSummary = Pick<Product, 'id' | 'name' | 'price'>;
// { id: string; name: string; price: number; }
type ProductDetails = Omit<Product, 'id'>;
// { name: string; price: number; description: string; }
#4
Term
What are Conditional Types in TypeScript? Provide an example.
Definition
Conditional types allow you to choose one of two possible types based on a condition that takes the form of a type relationship test. They use the extends keyword in a ternary operator-like structure: SomeType extends OtherType ? TrueType : FalseType;.
typescript3 lines
type IsString<T> = T extends string ? "Yes" : "No";
type A = IsString<string>; // "Yes"
type B = IsString<number>; // "No"
#5
Term
Explain Mapped Types and their common use cases.
Definition
Mapped types allow you to create new types by transforming the properties of an existing type. They iterate over keys of a type using [P in K], where P is a new property name and K is a union of string literals (often keyof T). They are fundamental to utility types like Partial, Readonly, etc.
typescript7 lines
type Nullable<T> = { [P in keyof T]: T[P] | null };
interface User {
  name: string;
  age: number;
}
type NullableUser = Nullable<User>;
// { name: string | null; age: number | null; }
#6
Term
When should you use an interface versus a type alias in TypeScript?
Definition
  • interface: Primarily used for defining the shape of objects, classes, and function types. They support declaration merging (can be reopened to add new members) and are generally preferred for public APIs and object shapes.
  • type alias: Can define types for primitives, unions, tuples, and also object shapes. They do not support declaration merging. They are more versatile for complex type manipulations (e.g., conditional types, mapped types).
In many simple object shape cases, they are interchangeable, but interface often offers better performance and error reporting in complex scenarios.
#7
Term
What are Generics in TypeScript and why are they important?
Definition
Generics allow you to write components that can work with a variety of types instead of a single one. They provide a way to make functions, classes, and interfaces reusable and type-safe by parameterizing types. This reduces code duplication and improves flexibility while maintaining strong type checking.
typescript5 lines
function identity<T>(arg: T): T {
  return arg;
}
let output = identity<string>("myString"); // output is string
let output2 = identity(123); // output2 is number (type inference)
#8
Term
How do you add constraints to a generic type parameter?
Definition
Generic constraints limit the types that can be used with a generic component. You use the extends keyword to specify that a type parameter must be a subtype of another type. This allows you to safely access properties or methods on the generic type.
typescript9 lines
interface Lengthwise {
  length: number;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
  console.log(arg.length); // Now 'length' is guaranteed to exist
  return arg;
}
// loggingIdentity(3); // Error: Argument of type 'number' is not assignable to parameter of type 'Lengthwise'.
loggingIdentity({ length: 10, value: 3 }); // OK
Showing 8 of 22 cards in this deck.
Study All Now