String TypeProblem Statement'Write a function greet(name: string): string that returns "Hello, name!".'Approach 1. Specify the type for the parameter `name` as `string`. 2. Specify the return type as `string`. Solution`function greet(name: string): string { return `Hello, ${name
Number AdditionProblem Statement'Write a function add(a: number, b: number): number that returns their sum.'Approach 1. Tag both `a` and `b` as `number`. 2. Tag the return value as `number`. Solution`function add(a: number, b: number): number { return a + b;
Array of StringsProblem Statement'Write a function logNames(names: string[]): void that logs each name in the array.'Approach 1. Use `string[]` or `Array<string>` for the parameter. 2. The return type should be `void` since it only logs. Solution`function logNames(names: string[]): void { names.forEach(name => console.log(name));
Basic InterfaceProblem Statement'Define an interface User with name (string) and age (number). Write a function isAdult(user: User): boolean.'Approach 1. Use the `interface` keyword. 2. Define the types for the properties. Solution`interface User { name: string; age: number;
Optional PropertiesProblem Statement'Update the User interface to make age optional. Write a function getName(user: User): string.'Approach 1. Use `age?:` to make it optional. Solution`interface User { name: string; age?: number;
Union TypesProblem Statement'Write a function formatId(id: string | number): string that converts the input to a string.'Approach 1. Use the pipe operator `|` for union types. 2. Use `.toString()` to ensure the return is a string. Solution`function formatId(id: string | number): string { return id.toString();
Type AliasProblem Statement'Create a type alias Point with x and y as numbers. Write a function move(p: Point): Point.'Approach` 1. Use `type Point = { ...Solution`type Point = { x: number; y: number;
Tuple DefinitionProblem Statement'Define a tuple type Response that contains a string and a boolean.'Approach 1. Tuples use fixed-length array syntax: `[string, boolean]`. Solutiontype Response = [string, boolean]; const myRes: Response = ["Success", true];
Avoiding AnyProblem Statement'Write a function process(val: unknown) and use a type guard to log the value IF it is a string.'Approach 1. `unknown` is safer than `any`. 2. Use `typeof val === "string"`. Solution`function process(val: unknown): void { if (typeof val === "string") { console.log(val.toUpperCase());
Literal TypesProblem Statement'Write a function setDirection(dir: "up" | "down" | "left" | "right") that logs the direction.'Approach 1. Literal types restrict values to specific occurrences. Solution`function setDirection(dir: "up" | "down" | "left" | "right"): void { console.log(`Moving ${dir
Readonly PropertiesProblem Statement'Create an interface Config with a readonly property apiKey: string.'Approach 1. Use the `readonly` keyword before the property name. Solution`interface Config { readonly apiKey: string; url: string;
Type InferenceProblem Statement'Declare a variable score and initialize it to 10. What is its inferred type?'Approach 1. TypeScript automatically assigns types based on initial values. Solutionlet score = 10; // Inferred as number
Type AssertionProblem Statement'Cast an unknown value status to string using the "as" syntax.'Approach 1. Use `val as string` or `<string>val`. Solution`function logStatus(status: unknown) { const strStatus = status as string; console.log(strStatus.length);
Intersection TypesProblem Statement'Combine two types Person and Employee into a new type Staff using intersection.'Approach 1. Use the `&` operator. Solution`type Person = { name: string
Basic EnumProblem Statement'Define an enum Status with values Pending, Active, and Completed.'Approach 1. Use the `enum` keyword. Solution`enum Status { Pending, Active, Completed
Const EnumProblem Statement'Define a const enum Colors with Red, Green, Blue. Explain why use const.'Approach 1. `const enum` is removed during compilation, literals are inlined. Solution`const enum Colors { Red = "#FF0000", Green = "#00FF00", Blue = "#0000FF"
Never TypeProblem Statement'Write a function throwError(msg: string): never that always throws an error.'Approach 1. `never` represents values that never occur (like an infinite loop or throw). Solution`function throwError(msg: string): never { throw new Error(msg);
Interface ExtensionProblem Statement'Create an interface Animal and extend it to a Dog interface with a breed property.'Approach 1. Use the `extends` keyword. Solution`interface Animal { species: string;
Function OverloadingProblem Statement'Declare overloads for a function makeDate that takes a timestamp (number) OR three numbers (y, m, d).'Approach 1. Provide signatures without implementation first. 2. Provide the implementation with a compatible signature. Solution`function makeDate(timestamp: number): Date; function makeDate(y: number, m: number, d: number): Date; function makeDate(yOrTimestamp: number, m?: number, d?: number): Date { if (m !== undefined && d !== undefined) { return new Date(yOrTimestamp, m, d);
Index SignaturesProblem Statement'Create an interface Dictionary that allows any number of string keys with string values.'Approach 1. Syntax: `[key: string]: string`. Solution`interface Dictionary { [key: string]: string;