JavaScript & TypeScript
Types, asynchronous code, modules and JavaScript runtime edge cases.
100 troubleshooting guides · Page 3 / 5
Why does string indexing corrupt an emoji while building initials?
Bracket indexing returns one UTF-16 code unit.
Why does a global validation regex alternate true and false on identical input?
The g and y flags retain lastIndex between calls.
How do I search literal user text containing brackets and backslashes?
Prefer includes for literal substring searches.
Why does replacing a repeated placeholder update only the first occurrence?
A string passed to replace matches once.
Why does a replacement containing $& unexpectedly duplicate matched text?
Replacement strings interpret special dollar patterns.
Why does splitting on a grouped delimiter include the delimiter in output?
Capturing groups are included in split results.
Is parsed JSON safe to merge into nested application settings?
Parsing checks syntax, not allowed configuration keys.
Why does structuredClone fail after a callback is added to my state?
Functions are not supported by structured cloning.
Why can I not list cached entries stored in a WeakMap?
WeakMap exposes neither enumeration nor size because key lifetime depends on garbage collection.
Why do all typed-array views stop working after transferring one buffer?
Transferring the underlying ArrayBuffer detaches it from the sender, affecting every view over it.
Why does deleting and re-adding Map keys during iteration repeat work?
Iteration can visit entries added before it finishes.
How do I avoid misparsing Turkish decimal commas in price inputs?
Intl.NumberFormat formats but does not provide a general parser.
Why does catch error.message fail after enabling strict TypeScript?
JavaScript can throw any value, so the caught value is unknown.
Why does Array.find return a possibly undefined record despite a known ID?
The type system cannot guarantee the array contains that ID.
Why does array[index] become possibly undefined after a compiler setting change?
noUncheckedIndexedAccess accounts for out-of-range access.
Why can I omit an optional property but not assign undefined to it?
With exactOptionalPropertyTypes, absence differs from a present undefined value unless the type explicitly allows undefined.
Why does Object.keys not produce keys safe for indexing my typed object?
Runtime objects may contain keys beyond their static type.
Why does config.mode require bracket access after stricter dictionary typing?
The property is known only through an index signature.
Why does an empty TypeScript array reject the first object pushed into it?
Empty-array inference depends on context and can produce never[] in a generic initializer.
How do I make a TypeScript switch fail compilation when a new status is added?
Use a discriminated union and assign the remaining value to never in the default branch: const unreachable: never = state.
Why does event.target.value fail in a TypeScript DOM handler?
EventTarget is not necessarily an input, and bubbling can originate from descendants.
How does satisfies validate configuration without widening every literal?
On TypeScript 4.9+, use const config = {...} satisfies Config to check compatibility while retaining useful inferred detail.
Why does a literal action type widen to string before dispatch?
A mutable object property may be inferred as string.
How do I preserve a literal tuple through a generic helper?
On TypeScript 5.0+, a const type parameter can improve literal inference: function define<const T extends readonly string[]>(x:T){return x;}.
Ask your own question · Sign in to post questions and answers.