Recommended Free Tools
To empty a mutable TypeScript array without changing its declared element type, set arr.length = 0. This clears the existing array object in place; the variable remains, for example, a string[], ready to accept strings again.
Clear a mutable array in place
Assigning zero to an ordinary JavaScript array’s writable length removes its elements. TypeScript’s annotation describes the array’s static element type, not how many items it currently contains.
const names: string[] = ["Ada", "Grace"];
names.length = 0;
names.push("Linus"); // accepted: names is still string[]
The binding declared with const cannot be reassigned, but its array contents can still be mutated. The JavaScript behavior is documented in MDN’s Array.length reference; TypeScript’s Handbook discussion of object and array types explains the static typing.
Choose between clearing and replacing
| Goal | Operation | Effect |
|---|---|---|
| Empty the existing array object | arr.length = 0 |
Mutates the same array; its length becomes zero. |
| Remove elements using an array method | arr.splice(0, arr.length) |
Removes the selected elements from the original array in place. |
| Use a different empty array | arr = [] (when the binding is mutable) |
Replaces the binding’s value; references to the old array still point to that old array. |
splice() is an alternative when you want to express removal through an array method:
#1 Best Overall
const names: string[] = ["Ada", "Grace"];
names.splice(0, names.length);
MDN documents splice() as a mutating operation in its Array.prototype.splice() reference. Use either in-place operation when other code should continue to observe the same array object.
Readonly references cannot be cleared through that type
TypeScript’s readonly T[] and ReadonlyArray<T> types prevent mutation through that reference. Setting length or calling splice() on it is a type error because those operations mutate the array.
Rank #2
- TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
- TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
function clear(values: string[]): void {
values.length = 0;
}
function cannotClear(values: readonly string[]): void {
// values.length = 0; // TypeScript error
// values.splice(0); // TypeScript error
}
If clearing is part of a function’s job, give it a mutable array parameter, such as string[]. A type assertion can silence a static restriction, but it does not change the runtime object or make a frozen object mutable. TypeScript’s type assertions documentation describes assertions as a compile-time aid, not a runtime conversion.
Do not use a fixed tuple for a collection you empty
A tuple such as [string, number] expresses fixed positions and element types. Clearing it at runtime does not change its declared type into []; TypeScript may still treat the value according to the tuple’s positional declaration. If emptying is a normal operation, use a general mutable array type instead, such as (string | number)[].
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →TypeScript documents tuple and readonly tuple behavior in its 3.4 release notes and its Handbook array discussion.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




