Flashcard Deck · 23 cards · Public

TypeScript - Advanced Types, Interfaces & Generics

Master TypeScript's advanced features with this comprehensive flashcard deck! Dive deep into generics, complex type manipulations like mapped and conditional types, and robust type guards to build highly scalable and maintainable applications.

Cards in this deck

(23 cards)

Preview terms and definitions before starting your study session.

#1
Term
What are Generics in TypeScript?
Definition
Generics provide a way to create reusable components that can work with a variety of types rather than a single one. They allow you to write code that is flexible and type-safe, enabling components to consume and produce values of any type.
#2
Term
Example of a Generic Function
Definition
A generic function uses a type variable (e.g., T) to make the function work with different types while preserving type safety.
typescript6 lines
function identity<T>(arg: T): T {
  return arg;
}

let output1 = identity<string>("myString"); // type of output1 is string
let output2 = identity<number>(100);     // type of output2 is number
#3
Term
Example of a Generic Interface
Definition
Generic interfaces can describe the shape of an object that works with a generic type.
typescript6 lines
interface Box<T> {
  value: T;
}

let stringBox: Box<string> = { value: "hello" };
let numberBox: Box<number> = { value: 123 };
#4
Term
Example of a Generic Class
Definition
Generic classes are useful for creating data structures or utility classes that operate on a specific type, which is determined at instantiation.
typescript12 lines
class GenericNumber<T> {
  zeroValue: T;
  add: (x: T, y: T) => T;

  constructor(zeroValue: T, addFunction: (x: T, y: T) => T) {
    this.zeroValue = zeroValue;
    this.add = addFunction;
  }
}

let myGenericNumber = new GenericNumber<number>(0, (x, y) => x + y);
console.log(myGenericNumber.add(5, 10)); // 15
#5
Term
What are Generic Constraints?
Definition
Generic constraints allow you to restrict the types that a generic type parameter can be. This is useful when you need to perform operations on the generic type that are only available on a subset of types.
typescript11 lines
interface Lengthwise { 
  length: number; 
}

function loggingIdentity<T extends Lengthwise>(arg: T): T {
  console.log(arg.length); // Now we know 'arg' has a .length property
  return arg;
}

loggingIdentity({ length: 10, value: 3 }); // OK
// loggingIdentity(3); // Error: Argument of type 'number' is not assignable to parameter of type 'Lengthwise'.
#6
Term
What are Intersection Types?
Definition
Intersection types combine multiple types into one. An object of an intersection type will have all the members of all the types it intersects. It's like an AND operator for types.
typescript9 lines
type Draggable = { drag: () => void };
type Resizable = { resize: () => void };

type UIWidget = Draggable & Resizable;

let widget: UIWidget = {
  drag: () => console.log('Dragging...'),
  resize: () => console.log('Resizing...')
};
#7
Term
What are Union Types?
Definition
Union types allow a value to be one of several types. It's like an OR operator for types. If a value has a union type, you can only access members that are common to all types in the union, unless you use type guards.
typescript8 lines
type StringOrNumber = string | number;

function printId(id: StringOrNumber) {
  console.log(id);
}

printId(101); // OK
printId("abc"); // OK
#8
Term
What are Type Guards?
Definition
Type guards are special expressions that perform a runtime check to guarantee the type of a variable within a certain scope. They are used to narrow down the type of a variable from a union type to a more specific type.
Showing 8 of 23 cards in this deck.
Study All Now