Find the index, check it, then splice: const i = arr.indexOf(item); if (i !== -1) arr.splice(i, 1);. That changes the array in place. For a new array, write arr.filter((x) => x !== item), which also removes every match. On a readonly array splice does not compile; use arr.toSpliced(i, 1) or filter there.
At runtime a TypeScript array is a JavaScript array, so the tools are the JavaScript ones: splice to remove in place, filter and toSpliced for a copy. The JavaScript version of this page covers how they behave, including why delete arr[i] leaves a hole and how to remove items inside a loop without skipping any.
What TypeScript adds is the types around them: a generic helper that rejects an item of the wrong type, a filter whose result type narrows, readonly arrays that have no splice at all, and removal methods whose return type tells you the array may have been empty. Each example runs on this page: hit Run, then edit the code and run it again.
1indexOf + splice in a typed helperRecommended
Arrays have no remove(item) method, so most projects write one. Make it generic: removeItem<T>(arr: T[], item: T) ties the item's type to the array's, so removeItem(fruits, 42) on a string[] is rejected with error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. Inside, indexOf finds the first === match and splice(i, 1) removes it, shifting the rest left. For objects, indexOf compares references, so look the index up with findIndex and a predicate instead.
// Removes the first item === item, in place. Reports whether it found one. function removeItem<T>(arr: T[], item: T): boolean { const i = arr.indexOf(item); if (i === -1) return false; // splice(-1, 1) would remove the LAST item arr.splice(i, 1); return true; } const fruits: string[] = ['apple', 'banana', 'cherry', 'banana']; console.log(removeItem(fruits, 'banana'), fruits); console.log(removeItem(fruits, 'mango'), fruits); // Objects are compared by reference: find the index with a predicate interface Task { id: number; title: string; } const tasks: Task[] = [ { id: 1, title: 'Write' }, { id: 2, title: 'Test' }, { id: 3, title: 'Ship' }, ]; const at = tasks.findIndex((task) => task.id === 2); if (at !== -1) { const removed: Task[] = tasks.splice(at, 1); // splice returns T[] console.log(removed); } console.log(tasks.map((task) => task.title));
Output
Prints true [ 'apple', 'cherry', 'banana' ]: only the first banana went. The missing mango gives false [ 'apple', 'cherry', 'banana' ] with nothing removed, which is what the -1 check is for: splice(-1, 1) counts from the end and would have deleted the last item. splice returns the removed elements as a Task[] ([ { id: 2, title: 'Test' } ]), and the array shrinks to [ 'Write', 'Ship' ].
2filter(): by value, by id or by type
filter builds a new array from the items its callback accepts and leaves the original alone. It is the answer when you want every match gone, when you match objects by a property, and in Angular, React or Redux code where you replace the array instead of changing it (this.items = this.items.filter(…)). TypeScript keeps the element type, and since version 5.5 it also narrows it: a callback like (x) => typeof x === 'number' is inferred as a type guard, so removing the strings from a (string | number)[] leaves a number[].
interface User { id: number; name: string; } const users: User[] = [ { id: 1, name: 'Ada' }, { id: 2, name: 'Linus' }, { id: 3, name: 'Grace' }, ]; // filter keeps what the callback accepts, so "remove id 2" is id !== 2 const remaining: User[] = users.filter((user) => user.id !== 2); console.log(remaining.map((user) => user.name), users.length); // Every occurrence of a value, not just the first const tags: string[] = ['ts', 'css', 'ts', 'html', 'ts']; console.log(tags.filter((tag) => tag !== 'ts')); // Remove by type: since TypeScript 5.5 this callback is inferred as a // type guard, so the result is number[] rather than (string | number)[] const mixed: (string | number)[] = ['a', 1, 'b', 2]; const nums: number[] = mixed.filter((x) => typeof x === 'number'); console.log(nums, nums.reduce((sum, n) => sum + n, 0));
Output
Prints [ 'Ada', 'Grace' ] 3 (the original still has 3 users), then [ 'css', 'html' ], then [ 1, 2 ] 3. The last line compiles only because the result is number[]: the type guard is what lets it be assigned to nums and summed. On TypeScript before 5.5, write the guard yourself: (x): x is number => typeof x === 'number'. Inference also needs the callback to be a plain check; filter(Boolean) does not narrow. Type guards in general are covered in check a type at runtime.
3toSpliced(): a copy without the item
toSpliced(i, 1) (ES2023) takes the same arguments as splice but returns a new array and never touches the original. That makes it the way to remove by index from a readonly string[], a ReadonlyArray<T> or an as const tuple, none of which have splice (section 5). The copy it returns is an ordinary mutable T[], and so is the result of filter.
// A readonly array has no splice, but toSpliced and filter work on it const fruits: readonly string[] = ['apple', 'banana', 'cherry']; const i = fruits.indexOf('banana'); // toSpliced returns a plain string[] copy; guard -1 as with splice const without: string[] = i === -1 ? [...fruits] : fruits.toSpliced(i, 1); console.log(without); console.log(fruits); // untouched without.push('date'); // the copy is mutable console.log(without); // The same for an as const tuple: the copy is an array of the union const sizes = ['S', 'M', 'L', 'XL'] as const; const smaller: ('S' | 'M' | 'L' | 'XL')[] = sizes.filter((s) => s !== 'XL'); console.log(smaller, sizes.length);
Output
Prints [ 'apple', 'cherry' ], the untouched original [ 'apple', 'banana', 'cherry' ], then [ 'apple', 'cherry', 'date' ] after the push on the copy. Filtering the as const tuple gives [ 'S', 'M', 'L' ] 4, typed as an array of the four literals rather than the fixed tuple. toSpliced is in TypeScript's ES2023 lib, so a project whose lib or target is older does not know the method; there, copy the two halves with [...arr.slice(0, i), ...arr.slice(i + 1)].
4Which should you use?
| Method | Mutates | Removes | Best for |
|---|---|---|---|
| arr.splice(arr.indexOf(v), 1) | Yes | First match | One item in place (guard -1) |
| arr.filter((x) => x !== v) | No | Every match | All occurrences, by id, by type |
| arr.toSpliced(i, 1) | No | One index | readonly arrays, immutable state |
| arr.pop() / arr.shift() | Yes | Last / first | Stacks and queues (T | undefined) |
| delete arr[i] | Yes | Nothing (leaves a hole) | Never, on arrays |
5readonly arrays reject splice
A readonly string[] (also written ReadonlyArray<string>, and what as const gives you) is a type with only the non-mutating methods. There is no splice, push, pop or shift on it, so the in-place recipe from section 1 stops the build. This example fails on purpose: press Run and the checker rejects it before anything executes.
const fruits: readonly string[] = ['apple', 'banana', 'cherry']; // readonly arrays only have the methods that don't change the array const i = fruits.indexOf('banana'); fruits.splice(i, 1); console.log(fruits);
Output
The run stops with error TS2551: Property 'splice' does not exist on type 'readonly string[]'. Did you mean 'slice'? On an as const array the type in the message is the tuple, such as readonly ["x", "y"]. Don't cast the readonly away: whoever typed it that way is relying on the array not changing under them. Make a copy instead with toSpliced or filter (section 3), or, if you own the data, declare it as a plain string[].
6The type of the removed item
pop() and shift() return T | undefined, because the array may be empty. With strict on, as in most projects, const last: string = stack.pop(); fails with error TS2322: Type 'string | undefined' is not assignable to type 'string'. This page's compiler runs without strict, so it would accept that line; the fix is the same either way: compare with undefined first and the type narrows. splice is the opposite case. It returns T[], and pulling the first element out of it is typed T even when nothing was removed.
// shift() and pop() return string | undefined: the array may be empty function nextJob(queue: string[]): string { const job = queue.shift(); if (job === undefined) return '(queue empty)'; return job.toUpperCase(); // narrowed to string } const queue: string[] = ['build', 'test']; console.log(nextJob(queue), nextJob(queue), nextJob(queue)); // splice() returns string[], so a destructured item is typed string // even when nothing was removed. The checker can't catch this one. const [removed] = queue.splice(0, 1); console.log(removed, typeof removed); // Check what splice returned instead const out: string[] = ['lint'].splice(0, 1); console.log(out.length > 0 ? `removed ${out[0]}` : 'nothing removed');
Output
Prints BUILD TEST (queue empty): the third shift() found an empty queue and the undefined check handled it. The destructured removed is typed string but prints undefined undefined, and calling removed.toUpperCase() would throw at runtime with no warning from the checker, even with strict. Check the length of the array splice returned instead, as the last line does (removed lint). More ways to handle a value that may be missing are in check for null or undefined.
Frequently asked questions
How do I remove a specific item from an array in TypeScript?
Find its index and splice it out: const i = arr.indexOf(item); if (i !== -1) arr.splice(i, 1);. That removes the first match in place. For a new array, use arr.filter((x) => x !== item), which removes every match, or arr.toSpliced(i, 1) for one index. Wrapping the first in function removeItem<T>(arr: T[], item: T): boolean makes the checker reject an item of the wrong type.
How do I remove an object from an array by its id in TypeScript?
For a new array, users.filter((u) => u.id !== id) keeps the User[] type. To remove it in place, find the index with a predicate and splice: const i = users.findIndex((u) => u.id === id); if (i !== -1) users.splice(i, 1);. indexOf does not work for a lookalike object, because objects are compared by reference.
Why does TypeScript say Property 'splice' does not exist on type 'readonly string[]'?
The array is typed readonly (or comes from as const), and readonly arrays only have the methods that do not change them. The full error is error TS2551: Property 'splice' does not exist on type 'readonly string[]'. Did you mean 'slice'?. Use arr.toSpliced(i, 1) or arr.filter(...), which return a new mutable array, or change the declaration to string[] if the array is yours to change.
Why does indexOf on an as const array reject my string?
On const roles = ['admin', 'user'] as const, indexOf only accepts the literal types, so passing a string fails with error TS2345: Argument of type 'string' is not assignable to parameter of type '"admin" | "user"'. Widen the array for the lookup: (roles as readonly string[]).indexOf(input).
Can I use delete to remove an array element in TypeScript?
TypeScript allows delete arr[i], even with strict on, but it only empties the slot: ['a', 'b', 'c'] becomes [ 'a', <1 empty item>, 'c' ] with a length of 3. The element type does not change either, so the checker still treats the hole as a string. Use splice, toSpliced or filter on arrays.
How do I remove items from an array while looping over it?
Use filter and reassign the result, which is a single pass and cannot skip items. If the same array object must shrink in place, loop backwards with for (let i = arr.length - 1; i >= 0; i--) and splice: a forward loop skips the element that slides into the removed slot. The JavaScript version of this page shows the skipped item happening.
Run it yourself
Open any of these in the full TypeScript editor to tweak, run and share.
TypeScript playground