Fixing the never Type Error from useState([]) in TypeScript
In React, if you start a state that holds list data with useState([]) as an empty array, TypeScript may report error TS2339: Property 'id' does not exist on type 'never'. The cause is that an empty array gives no clue about its element type, so the state's type becomes never[] (an array that cannot hold any element). Writing the element type as a type argument, as in useState<Todo[]>([]), makes the error go away.
This happens with a state that starts as an empty array and gets filled in later, such as one that receives data from an API. In my test environment I triggered this error with both tsc and the TypeScript language server, and I confirmed the full message and that adding the type removes the error.
The test environment was Ubuntu 24.04 with Node.js 22.23.4. I used TypeScript 7.0.2, React 19.3.1, and @types/react 19.3.0. For comparison, I also ran tsc on the same files with TypeScript 5.9.3. The tsconfig.json was generated by npx tsc --init and includes "strict": true. I checked the errors shown in the editor by running the TypeScript 7 language server directly (tsc --lsp --stdio), not through the editor itself such as VS Code. I did not test how the errors look in the editor UI or how the page behaves when run in a browser.
The full error
In src/TodoList.tsx, I wrote a component that loads an array of Todo items passed in as a prop into state when a button is clicked, then displays them in a list. The state is created with useState([]), without a type, starting from an empty array.
import { useState } from "react";
type Todo = { id: number; title: string };
export function TodoList({ initial }: { initial: Todo[] }) {
const [todos, setTodos] = useState([]);
function load() {
setTodos(initial);
}
return (
<>
<button onClick={load}>読み込む</button>
<ul>
{todos.map((todo) => (
<li key={todo.id}>{todo.title}</li>
))}
</ul>
</>
);
}
Running npx tsc --noEmit produced the following output (exit code 1).
src/TodoList.tsx(9,14): error TS2345: Argument of type 'Todo[]' is not assignable to parameter of type 'SetStateAction<never[]>'.
Type 'Todo[]' is not assignable to type 'never[]'.
Type 'Todo' is not assignable to type 'never'.
src/TodoList.tsx(17,25): error TS2339: Property 'id' does not exist on type 'never'.
src/TodoList.tsx(17,35): error TS2339: Property 'title' does not exist on type 'never'.
There are two kinds of errors. TS2339 means that the never type has no property id (or title), and it is reported on the lines with todo.id and todo.title. TS2345 means that Todo[] cannot be passed to an argument of type SetStateAction<never[]>, and it is reported on the line setTodos(initial).
With --pretty, the locations are shown with squiggly underlines. (I removed the control characters used for colors and omitted the final summary line.)
src/TodoList.tsx:9:14 - error TS2345: Argument of type 'Todo[]' is not assignable to parameter of type 'SetStateAction<never[]>'.
Type 'Todo[]' is not assignable to type 'never[]'.
Type 'Todo' is not assignable to type 'never'.
9 setTodos(initial);
~~~~~~~
src/TodoList.tsx:17:25 - error TS2339: Property 'id' does not exist on type 'never'.
17 <li key={todo.id}>{todo.title}</li>
~~
src/TodoList.tsx:17:35 - error TS2339: Property 'title' does not exist on type 'never'.
17 <li key={todo.id}>{todo.title}</li>
~~~~~
Both errors are reported on lines that use the state. The cause, however, is on line 6, in useState([]).
The language server reports the same three errors
The tsc in TypeScript 7 runs as a language server when given --lsp --stdio (I started it in the test environment to confirm this). The typescript-go repository README, which is the codebase used for the TypeScript 7 port, describes the LSP language service and a preview VS Code extension. I started the TypeScript 7.0.2 language server with tsc --lsp --stdio and had it open src/TodoList.tsx using the LSP procedure. The diagnostics it returned, and the type information (hover) for todos and todo at specified positions, are shown below. I printed the returned JSON one item at a time with a small script I wrote. I ran the hover checks twice with different positions. The diagnostics and the server line were the same both times, so I show them only once.
server: typescript-go 7.0.2
diagnostics (3):
9:14-9:21 severity=1 source=ts code=2345 Argument of type 'Todo[]' is not assignable to parameter of type 'SetStateAction<never[]>'.
Type 'Todo[]' is not assignable to type 'never[]'.
Type 'Todo' is not assignable to type 'never'.
17:25-17:27 severity=1 source=ts code=2339 Property 'id' does not exist on type 'never'.
17:35-17:40 severity=1 source=ts code=2339 Property 'title' does not exist on type 'never'.
hover 6:10: const todos: never[]
hover 16:21: (parameter) todo: never
severity=1 is the value that the LSP specification uses for errors. The message text was the same as tsc. In the hover results, the type of todos on the useState line is never[], and the type of the argument todo in the function passed to map is never.
An empty array makes the type never[]
In @types/react, useState is defined as follows (I copied this from node_modules/@types/react/index.d.ts in the test environment).
function useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>];
If no type argument is written, the state type S is determined from the initial value. The TypeScript Handbook's explanation of generics (English) calls this type argument inference. The React documentation on typing useState in TypeScript (English) also gives an example where useState(false) becomes the boolean type.
When the initial value is [], there are no elements, so there is no clue for the element type. In my test environment with "strict": true, S became never[] in this case. never is the type for values that never occur (the Handbook's explanation of never (English)). Because the elements are never, reading any property gives TS2339, and trying to store a Todo[] gives TS2345.
The same thing happens without React. For comparison, I wrote a case where an empty array is stored in a variable, and a case where an empty array is passed to a generic function keep that returns its argument as it is.
function keep<T>(value: T): T {
return value;
}
const list = [];
list.push({ id: 1 });
const kept = keep([]);
kept.push({ id: 1 });
With tsc, only the kept.push line produced error TS2345: Argument of type '{ id: number; }' is not assignable to parameter of type 'never'. The hover types were any[] for list and never[] for kept. Storing an empty array in a variable like const list = [] does not produce an error, but when [] is passed to a generic function that infers its type argument, the type becomes never[], just as with useState.
Writing the element type in useState fixes it
The fix is to write the state's type as a type argument on the useState call on line 6.
- const [todos, setTodos] = useState([]);
+ const [todos, setTodos] = useState<Todo[]>([]);
After changing only this line and running npx tsc --noEmit, there was no output and the exit code was 0. The language server reported 0 diagnostics, and the hover types changed as follows. (As before, I combined the two runs and omitted the server line.)
diagnostics (0):
hover 6:10: const todos: Todo[]
hover 16:21: (parameter) todo: Todo
With TypeScript 5.9.3, the original file produced the same three errors with the same text, and the fixed file produced no errors. However, the exit code of tsc when there were errors was 2 with 5.9.3 and 1 with 7.0.2.
Once the type is written, typos in property names are also caught. When I ran tsc on a file where todo.title was misspelled as todo.titel, it reported the following error.
src/TodoList.tsx(17,35): error TS2551: Property 'titel' does not exist on type 'Todo'. Did you mean 'title'?
Turning off strictNullChecks removes the errors but gives any[]
When I disabled strictNullChecks in the configuration generated by tsc --init (I also disabled exactOptionalPropertyTypes, because it cannot be specified without strictNullChecks and gives error TS5052), the original file no longer produced errors. In that case, the hover type of todos in the language server was any[].
With any[], reading properties of the elements is not type-checked. In the same configuration, the file where todo.title was misspelled as todo.titel also produced no error from tsc. Do not loosen the settings to make the errors go away; write the type as useState<Todo[]>([]) instead.
If the never error appears on a line that uses the state, follow the variable back to the line where the state is created and check whether its type is never[]. That shows where to make the fix.




