tezvyn:

TypeScript & Web APIs

TypeScript, browser APIs, WebAssembly, PWAs

246 bites

More in TypeScript & Web APIs — page 4

What is the core difference between localStorage and sessionStorage?
TypeScript & Web APIs2 min read

What is the core difference between localStorage and sessionStorage?

WHAT IT TESTS: Tab isolation versus origin-wide persistence. ANSWER OUTLINE: sessionStorage dies with its tab; localStorage survives restarts and shares across tabs. Example: checkout form versus theme preference. RED FLAG: Claiming persistence time differs.

TypeScript & Web APIs2 min read

Write a generic CreateSetters<T> that maps properties to setter methods

This tests TypeScript mapped types with key remapping and template literals. It iterates keyof T, remaps via as set plus Capitalize of string and K, and types values as setter methods. Red flag: manually typing setters or omitting string and K in Capitalize.

TypeScript & Web APIs2 min read

Implement recursive DeepReadonly<T> for nested objects and arrays

Tests recursive mapped types and conditional type narrowing. A strong answer uses a conditional to split arrays into ReadonlyArray, objects into readonly mapped types, and leaves primitives untouched.

TypeScript & Web APIs2 min read

Write a generic PickByValue<T, V> type

Tests conditional types and mapped-type key remapping. Great answer: K in keyof T as T[K] extends V ? K : never with value T[K]. Red flag: writing V extends T[K] instead, which reverses the assignability check and includes wrong keys.

TypeScript & Web APIs2 min read

Implement the built-in NonNullable<T> utility type from scratch

Tests conditional type distribution and union filtering. A strong answer uses distributive conditional types to map null and undefined to never while preserving other union members.

TypeScript & Web APIs2 min read

Implement MyParameters<T> using conditional types and infer

Tests if you can extract parameter types with infer. A strong answer matches T against a callable signature, uses infer to bind parameters into a tuple, and defaults to never for non-functions. Red flag: using any or indexing without inference.

TypeScript & Web APIs2 min read

Create TerminalStatus from Status using Exclude or Extract

This tests conditional type mechanics and whitelist versus blacklist thinking. Extract selects matches by assignability; Exclude removes others. Prefer Extract when the desired set is explicit. Red flag: claiming they are equivalent or only knowing Exclude.

TypeScript & Web APIs2 min read

Write a generic MakeOptional<T> mapped type without using Partial

This tests mapped type mechanics and the optionality modifier. A strong answer iterates over keyof T, adds the ? modifier, and preserves the original type via indexed access. A red flag is suggesting Object.assign or saying you would just use Partial.

TypeScript & Web APIs2 min read

Create a User type alias from a function return type

WHAT IT TESTS: TypeScript type-space introspection. ANSWER OUTLINE: Use typeof to grab the function type, then ReturnType to extract the return object shape. RED FLAG: Manually copying the object literal or confusing value-space and type-space typeof.

TypeScript & Web APIs2 min read

Describe a common use for Partial<T> and explain Required<T>

WHAT IT TESTS: Optional versus mandatory properties in TypeScript. ANSWER OUTLINE: Partial fits PATCH updates where only some fields arrive; Required strips optionality to enforce all properties. RED FLAG: Confusing with Pick or saying Partial allows nulls.

TypeScript & Web APIs2 min read

Explain Pick versus Omit with User examples

WHAT IT TESTS: Explicit selection versus type subtraction. ANSWER OUTLINE: Pick keeps listed keys; Omit drops them. Show User with id, name, email; derive Pick<User,"name"|"email"> and Omit<User,"id">. RED FLAG: Confusing them or calling one strictly safer.

TypeScript & Web APIs2 min read

Implement a generic PickByType<T, U> utility type

Tests mapped-type key filtering via conditional types and as remapping. Great answer: [K in keyof T as T[K] extends U ? K : never]: T[K]. Red flag: mapping all keys and setting values to never, which preserves keys instead of removing them.

TypeScript & Web APIs2 min read

Implement MyReturnType<T> using conditional types and infer

This tests type-level pattern matching with conditional types and infer. Answer uses T extends (...args: any[]) => infer R ? R : never, noting infer captures return slot, non-functions yield never. Red flag: conflating runtime values with compile-time types.

TypeScript & Web APIs2 min read

Write a generic fetchJSON<T> wrapper and explain its type safety benefits

Tests preserving type info across async boundaries via generics. Outline: write fetchJSON<T> returning Promise<T>, note response.json() is any, and show T lets callers lock in the response shape for compile-time checks.

TypeScript & Web APIs2 min read

Create a generic State class with getState and setState

WHAT IT TESTS: Binding a generic class parameter so methods share one consistent type. ANSWER OUTLINE: Write class State<T> with private T, constructor(T), getState(): T, and setState(T). RED FLAG: Using any instead of T, erasing type safety.

TypeScript & Web APIs2 min read

Write a generic type-safe getProperty using keyof

Command of TypeScript generics and keyof for compile-time property access. Declare generic T, accept key as keyof T, and return T[K]. RED FLAG: Using any or string for the key, which erases type safety and allows invalid properties.

TypeScript & Web APIs2 min read

Explain generic constraints and provide a length-constrained generic example

WHAT IT TESTS: Your grasp of bounding generics to shapes without losing type safety. ANSWER OUTLINE: Explain that extends requires properties; show T extends { length: number }; give a generic function signature. RED FLAG: Using any or dropping extends.

TypeScript & Web APIs2 min read

Refactor a function using generics to accept any array type

It tests generic type parameters for preserving element types through a function boundary. Answer: declare T, accept T[], return T, and let the compiler infer the element type from the call site.

Download a large file via fetch and track ReadableStream progress
TypeScript & Web APIs2 min read

Download a large file via fetch and track ReadableStream progress

It tests streaming I/O and backpressure beyond Promise-based fetch. Acquire a reader from response.body, loop read() until done, sum chunk lengths against Content-Length, and emit progress.

When does fetch trigger a CORS preflight, and what POST is complex?
TypeScript & Web APIs2 min read

When does fetch trigger a CORS preflight, and what POST is complex?

Tests whether you know the simple-request boundary. A strong answer names the three safe POST content-types and gives a cross-origin POST with application/json plus a custom header like Authorization.