Numeric EnumProblem Statement'Define an enum Direction with Up, Down, Left, Right. What are their default values?'Approach 1. Numeric enums start at 0 and increment. Solution`enum Direction { Up, // 0 Down, // 1 Left, // 2 Right // 3
Custom InitializerProblem Statement'Define an enum Response with No = 0 and Yes = 1.'Approach 1. You can manually assign values. Solution`enum Response { No = 0, Yes = 1
String EnumProblem Statement'Define a string enum Color with Red = "#FF0000" and Green = "#00FF00".'Approach 1. String enums require every member to be initialized with a string literal. Solution`enum Color { Red = "#FF0000", Green = "#00FF00"
Heterogeneous EnumProblem Statement'Can you mix string and numeric members in an enum? Show an example.'Approach 1. Yes, but it is generally discouraged. Solution`enum BooleanLike { No = 0, Yes = "YES"
Const Enum UsageProblem Statement'What is the primary benefit of using a const enum?'Approach 1. Performance and bundle size (they are inlined and removed from the JS output). Solution`const enum Size { Small, Large
Reverse MappingProblem Statement'Show how to get the name of an enum member from its value (numeric enums only).'Approach 1. Numeric enums support reverse mapping: `Enum[value]`. Solution`enum Status { Active = 1
String LiteralsProblem Statement'Create a type Alignment that only allows "left", "center", or "right".'Approach 1. Use union of string literals. Solutiontype Alignment = "left" | "center" | "right";
Numeric LiteralsProblem Statement'Create a type DiceRoll that only allows numbers 1 through 6.'Approach 1. Use union of numeric literals. Solutiontype DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;
Boolean LiteralsProblem Statement'Define a type that only allows true.'Approach 1. Literal types can also be booleans. Solutiontype OnlyTrue = true;
Literal InferenceProblem Statement'Why does let x = "hello" infer string, but const x = "hello" infer "hello"?'Approach 1. `const` values cannot change, so TS can narrow the type to the literal value. Solutionlet x = "hello"; // string const y = "hello"; // "hello"
Keyof EnumProblem Statement'How do you get a union of the keys of an enum?'Approach 1. Use `keyof typeof Enum`. Solution`enum Status { A, B
Object as LiteralProblem Statement'Use the "as const" assertion on an object to make all properties literal and readonly.'Approach 1. `as const` is the "const assertion". Solution`const config = { endpoint: "api.com", timeout: 5000
Switch ExhaustivenessProblem Statement'How can you ensure a switch statement handles all enum members?'Approach 1. Use the `never` type in the default case. Solution`enum Action { Save, Delete
Mapping LiteralsProblem Statement'Create a function that takes a string and returns a specific literal based on that string.'Approach 1. Use conditional types or overloads. Solution`function parse(s: string): "YES" | "NO" { return s === "y" ? "YES" : "NO";
Computed MembersProblem Statement'Can enum members have computed values? Show an example.'Approach 1. Yes, enums can use expressions for numeric values. Solution`enum Flags { None = 0, Read = 1 << 1, Write = 1 << 2
Discriminated UnionProblem Statement'Create a Result type using a literal "kind" property to distinguish Success and Error.'Approach 1. This is the pattern for sum types. Solution`type Result = | { kind: "success", data: string
Enum as Value and TypeProblem Statement'Demonstrate using an enum as both a type and a value.'Approach 1. Type in parameter declaration, value in the function body. Solution`enum Task { Urgent
Const Array AssertionProblem Statement'What is the type of ["a", "b"] as const?'Approach 1. It becomes a readonly tuple of literals. Solutionconst arr = ["a", "b"] as const; // type: readonly ["a", "b"]
Ambient EnumsProblem Statement'What is an ambient enum?'Approach 1. Used in declaration files (`.d.ts`) to describe the shape of existing enums. Solution`declare enum External { A = 1, B
Literal NarrowingProblem Statement'Write a function that narrows a string to a literal using an "if" check.'Approach 1. Simple equality check performs narrowing. Solution`function check(s: string) { if (s === "admin") { // s is now type "admin"