Problem Statement
Create a tuple-returning API (like useState) and type it precisely (readonly, optional element).
Explanation
Return a readonly tuple to prevent callers from swapping elements. Use labels and optional positions to improve DX. Example: `[value, setValue, reset?]` where `reset` may be omitted. Consumers get precise index types and IntelliSense.
Code Solution
SolutionRead Only
type Setter<T> = (v: T | ((p: T) => T)) => void;
function makeState<T>(initial: T): readonly [T, Setter<T>, (() => void)?] {
let state = initial;
const set: Setter<T> = v => { state = v instanceof Function ? v(state) : v };
const reset = () => { state = initial };
return [state, set, reset] as const;
}