Skip to content

Coding practice

TypeScript Coding Questions

20 hands-on challenges with prompts and solution sketches for TypeScript interview rounds.

Challenge set

20 coding questions

1. Typed Function

typescript

Type a sum function.

function sum(a: number, b: number): number {
  return a + b;
}

2. Interface vs Type

typescript

Model a User with an interface.

interface User {
  id: string;
  name: string;
  email?: string;
}

3. Union Narrowing

typescript

Narrow a status union.

type Status = "idle" | "loading" | "error";
function message(status: Status): string {
  if (status === "loading") return "Please wait";
  if (status === "error") return "Try again";
  return "Ready";
}

4. Generics Array

typescript

Write a generic first() helper.

function first<T>(items: T[]): T | undefined {
  return items[0];
}

5. Partial Update

typescript

Accept a Partial update object.

function updateUser(user: User, patch: Partial<User>): User {
  return { ...user, ...patch };
}

6. Readonly Props

typescript

Make props readonly.

type CardProps = Readonly<{
  title: string;
  count: number;
}>;

7. Record Map

typescript

Type a string-to-number map.

const scores: Record<string, number> = {
  html: 50,
  css: 80,
};

8. Discriminated Union

typescript

Model API result with a discriminant.

type Result =
  | { ok: true; data: string }
  | { ok: false; error: string };
function getData(result: Result) {
  return result.ok ? result.data : result.error;
}

9. keyof Lookup

typescript

Create a typed property getter.

function getProp<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}

10. Awaited Promise

typescript

Extract awaited type from a promise.

type User = Awaited<ReturnType<typeof fetchUser>>;

11. as const Object

typescript

Create a const enum-like object.

const Roles = {
  Admin: "admin",
  Editor: "editor",
} as const;
type Role = (typeof Roles)[keyof typeof Roles];

12. Type Guard

typescript

Write an isString type guard.

function isString(value: unknown): value is string {
  return typeof value === "string";
}

13. Utility Pick

typescript

Pick id and name from User.

type UserPreview = Pick<User, "id" | "name">;

14. Omit Password

typescript

Omit sensitive fields.

type PublicUser = Omit<User, "password">;

15. Function Overloads

typescript

Overload parse for string/number.

function parse(value: string): number;
function parse(value: number): string;
function parse(value: string | number): string | number {
  return typeof value === "string" ? Number(value) : String(value);
}

16. Satisfies Operator

typescript

Use satisfies for config typing.

const config = {
  retries: 3,
  mode: "safe",
} satisfies Record<string, string | number>;

17. Generic Component Props

typescript

Type a list component with generics.

type ListProps<T> = {
  items: T[];
  renderItem: (item: T) => React.ReactNode;
};

18. Zod-like Parse Shape

typescript

Type a runtime parse result.

type ParseResult<T> =
  | { success: true; data: T }
  | { success: false; error: string };

19. Template Literal Types

typescript

Build an event name type.

type EventName = `on${Capitalize<"click" | "focus">}`;

20. NonNullable Helper

typescript

Remove null/undefined from a type.

type Value = string | null | undefined;
type Clean = NonNullable<Value>;