Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript rejects a value passed to a useState setter

ErrorType 'number' is not assignable to type 'SetStateAction<undefined>'.

Short answer

useState gets its type from the initial value. Starting with useState(undefined) or useState(null) types the state as only undefined or null, so the setter refuses a number. Give useState an explicit union type, such as useState<number | null>(null). The same error appears when a component such as Mantine Select hands your setter a string or null.

  • Applies to React 19, TypeScript 5.x, Mantine 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Hover the state variable in your editor, or ask the compiler what type it inferred. If it says undefined or null alone, the cause is the initial value. If it says number but the value passed in is string | null, the cause is the component.

    npx tsc --noEmit
  2. 2Match what you see, then jump to its fix

    1. 1SetStateAction<undefined> in the message.The initial value is undefined, so the state type is only undefinedFix
    2. 2The error mentions the setter being passed to onChange of a Select.A Mantine Select gives your setter string | null, not numberFix

What does the error look like?

  • Type 'number' is not assignable to type 'SetStateAction<undefined>'.

Cause 1 of 2

The initial value is undefined, so the state type is only undefined

1Check

React infers the state type from the initial value. useState(undefined) therefore gives a state that can only hold undefined, and its setter accepts only undefined or an updater function that returns undefined.

grep -rn "useState(undefined)\|useState()" src/

2Fix

Name the type you want with a type argument, and include undefined or null for the empty state.

const [count, setCount] = useState<number | undefined>(undefined);
setCount(5);

3Verify

Run the type check; the error is gone and the editor shows number | undefined.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

Cause 2 of 2

A Mantine Select gives your setter string | null, not number

1Check

The Mantine Select documents its controlled value as string | null; the controlled example types the state as string | null. A number state does not match.

grep -rn "<Select" src/

2Fix

Keep the state as the select's type, and convert where you need a number.

const [value, setValue] = useState<string | null>(null);
const quantity = value === null ? undefined : Number(value);

<Select data={['1', '2', '3']} value={value} onChange={setValue} />

3Verify

Run the type check, then pick an option and confirm quantity is a number.

npx tsc --noEmit

Confirmed by mantine.dev

Did this fix it?

How do I stop it coming back?

  • Give every useState with an empty start an explicit type argument.
  • Check a component's documented value type before you wire your own state to it.
  • Run tsc --noEmit in CI so type errors show before a build.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.