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
typescriptType a sum function.
function sum(a: number, b: number): number {
return a + b;
} 2. Interface vs Type
typescriptModel a User with an interface.
interface User {
id: string;
name: string;
email?: string;
} 3. Union Narrowing
typescriptNarrow 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
typescriptWrite a generic first() helper.
function first<T>(items: T[]): T | undefined {
return items[0];
} 5. Partial Update
typescriptAccept a Partial update object.
function updateUser(user: User, patch: Partial<User>): User {
return { ...user, ...patch };
} 6. Readonly Props
typescriptMake props readonly.
type CardProps = Readonly<{
title: string;
count: number;
}>; 7. Record Map
typescriptType a string-to-number map.
const scores: Record<string, number> = {
html: 50,
css: 80,
}; 8. Discriminated Union
typescriptModel 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
typescriptCreate a typed property getter.
function getProp<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
} 10. Awaited Promise
typescriptExtract awaited type from a promise.
type User = Awaited<ReturnType<typeof fetchUser>>; 11. as const Object
typescriptCreate a const enum-like object.
const Roles = {
Admin: "admin",
Editor: "editor",
} as const;
type Role = (typeof Roles)[keyof typeof Roles]; 12. Type Guard
typescriptWrite an isString type guard.
function isString(value: unknown): value is string {
return typeof value === "string";
} 13. Utility Pick
typescriptPick id and name from User.
type UserPreview = Pick<User, "id" | "name">; 14. Omit Password
typescriptOmit sensitive fields.
type PublicUser = Omit<User, "password">; 15. Function Overloads
typescriptOverload 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
typescriptUse satisfies for config typing.
const config = {
retries: 3,
mode: "safe",
} satisfies Record<string, string | number>; 17. Generic Component Props
typescriptType a list component with generics.
type ListProps<T> = {
items: T[];
renderItem: (item: T) => React.ReactNode;
}; 18. Zod-like Parse Shape
typescriptType a runtime parse result.
type ParseResult<T> =
| { success: true; data: T }
| { success: false; error: string }; 19. Template Literal Types
typescriptBuild an event name type.
type EventName = `on${Capitalize<"click" | "focus">}`; 20. NonNullable Helper
typescriptRemove null/undefined from a type.
type Value = string | null | undefined;
type Clean = NonNullable<Value>;