Flashcard Deck · 24 cards · Public

TypeScript - Advanced Types & Tooling

Master TypeScript's advanced type system, from powerful generics and flexible utility types to robust type guards. Optimize your development workflow by understanding key tsconfig.json options and the nuances between interface and type.

Cards in this deck

(24 cards)

Preview terms and definitions before starting your study session.

#1
Term
What are TypeScript Generics (<T>) and why are they useful?
Definition
Generics allow you to write components that can work with a variety of types instead of a single one, providing flexibility while maintaining type safety. They enable you to create reusable functions, classes, and interfaces that adapt to the types they operate on.
#2
Term
Provide an example of a generic function in TypeScript.
Definition
typescript8 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
// Type argument inference often allows omitting <T>:
let output3 = identity("anotherString"); // type of output3 is string
#3
Term
How can generics be used with interfaces or type aliases?
Definition
Generics can define type parameters for interfaces or type aliases, making them reusable for different data types.
typescript6 lines
interface Box<T> {
  value: T;
}

let stringBox: Box<string> = { value: "hello" };
let numberBox: Box<number> = { value: 123 };
#4
Term
Explain the primary purpose of the interface keyword in TypeScript.
Definition
An interface in TypeScript is used to define the shape of an object, specifying the names and types of its properties and methods. It acts as a contract that classes can implement or objects can conform to, ensuring type consistency.
#5
Term
Explain the primary purpose of the type keyword (type alias) in TypeScript.
Definition
A type alias creates a new name for an existing type. It can be used to define primitive types, union types, intersection types, tuples, and even complex object shapes, similar to interfaces.
#6
Term
What is a key difference between interface and type regarding declaration merging?
Definition
interface supports "declaration merging," meaning if you declare multiple interfaces with the same name, TypeScript will merge them into a single interface. type aliases, however, do not support declaration merging; redeclaring a type with the same name will result in a compilation error.
#7
Term
When might you prefer using an interface over a type alias?
Definition
Prefer interface when defining the shape of an object, especially when you anticipate future extensions (e.g., via extends or declaration merging) or when implementing with classes (implements).
#8
Term
When might you prefer using a type alias over an interface?
Definition
Prefer type alias when defining primitive aliases, union types, intersection types, tuple types, or when you need to define a type that is not an object shape (e.g., type ID = string | number;).
Showing 8 of 24 cards in this deck.
Study All Now