Maaz Zindani

Loading the index…

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

React createContext in TypeScript: errors that come from the default value

Your error

Expected 1 arguments, but got 0.4 more lines it can show

Fix it fast

Most common cause, 1 of 4

createContext is called without an argument

Pass null and put null in the type, so the type tells the truth about the default.

Fix
import { createContext } from 'react';

type AuthValue = { user: User | null; signOut: () => void };

export const AuthContext = createContext<AuthValue | null>(null);
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Expected 1 arguments, but got 0, possibly null and SetStateAction<null> errors around React createContext come from its one required argument, the default value, which also sets the context type. Write createContext<AuthValue | null>(null), remove the null in a custom hook that throws without a provider, and type useState the same way. TypeScript 6 and 7, @types/react 19.3.

  • Applies to React 19.3, TypeScript 6.0, TypeScript 7.0, @types/react 19.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Find which line the first error sits on: the createContext call, a component that reads the context, the provider, or the code that calls the setter. The line decides which cause below to read.

Run this first
npx tsc --noEmit
grep -rn "createContext" src

Then find the line you see in this list and open its fix:

  1. 1createContext is called without an argumentExpected 1 arguments, but got 0. on the createContext line
  2. 2A null default makes every consumer see a possible nullProperty 'user' does not exist on type 'AuthValue | null'. or 'auth' is possibly 'null'. in components that call useContext
  3. 3The context type was inferred from a default that is too narrowThe provider's value prop fails with a message that names a type built from your default, for example a property typed null
  4. 4The state and setter behind the provider have types that are too narrowArgument of type 'User' is not assignable to parameter of type 'SetStateAction<null>'. where setUser is called, or a consumer cannot pass null or an updater function to the setter

What does the error look like?

  • Expected 1 arguments, but got 0.
  • Property 'user' does not exist on type 'AuthValue | null'.
  • 'auth' is possibly 'null'.
  • Type '{ user: User; signOut: () => void; }' is not assignable to type '{ user: null; signOut: () => void; }'.
  • Argument of type 'User' is not assignable to parameter of type 'SetStateAction<null>'.

Cause 1 of 4

createContext is called without an argument

You seeExpected 1 arguments, but got 0. on the createContext line

Check

Expected 1 arguments, but got 0 on a createContext line means the call has no default value, which the React types require. In the React type definitions, createContext takes a required parameter named defaultValue. Calling it with nothing is an arity error. React's own docs say to pass null when you have no meaningful default.

Check
grep -rn -E "createContext(<[^>]*>)?\(\)" src

Fix

Pass null and put null in the type, so the type tells the truth about the default.

Fix
import { createContext } from 'react';

type AuthValue = { user: User | null; signOut: () => void };

export const AuthContext = createContext<AuthValue | null>(null);

Verify

The error on the createContext line is gone.

Verify
npx tsc --noEmit

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

A null default makes every consumer see a possible null

You seeProperty 'user' does not exist on type 'AuthValue | null'. or 'auth' is possibly 'null'. in components that call useContext

Check

Property does not exist on type AuthValue | null, or possibly null, in a consumer comes from a context created with a null default. Once the context is created with a union that includes null, useContext returns that union. Reading a property from it is an error until null is ruled out, which is the behaviour the compiler is supposed to have here.

Check
grep -rn "useContext(AuthContext)" src

Fix

Do the check once, in a hook, and let components call the hook. The React docs recommend a runtime check that throws when the provider is missing.

Fix
import { useContext } from 'react';

export function useAuth(): AuthValue {
  const value = useContext(AuthContext);
  if (!value) {
    throw new Error('useAuth must be used within AuthProvider');
  }
  return value;
}

// in a component
const { user } = useAuth();

Verify

The component compiles, and rendering it outside the provider throws the message above instead of a vague null error.

Verify
npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

Cause 3 of 4

The context type was inferred from a default that is too narrow

You seeThe provider's value prop fails with a message that names a type built from your default, for example a property typed null

Check

Type '{ user: User; ... }' is not assignable to type '{ user: null; ... }' on the provider means the context type was inferred from a default that is too narrow. React's TypeScript guide says the type of the context value is inferred from the value you pass to createContext. A default such as { user: null } makes user the type null, so a real user does not fit.

Check
grep -rn -B1 -A3 "createContext(" src

Fix

Name the type and pass it as the generic argument, so the default has to fit the type and not the other way round.

Fix
// inferred from the default: user can only be null
const AuthContext = createContext({ user: null, signOut: () => {} });

// explicit
type AuthValue = { user: User | null; signOut: () => void };
const AuthContext = createContext<AuthValue>({ user: null, signOut: () => {} });

Verify

The provider accepts an object with a real user.

Verify
npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

Cause 4 of 4

The state and setter behind the provider have types that are too narrow

You seeArgument of type 'User' is not assignable to parameter of type 'SetStateAction<null>'. where setUser is called, or a consumer cannot pass null or an updater function to the setter

Check

SetStateAction<null> in a createContext setup means the provider's state was typed from its first value, null, so it can hold nothing else. React's TypeScript guide says useState reuses the initial value to pick the type. In the React type definitions the setter is Dispatch<SetStateAction<T>>, where SetStateAction<T> is a T or a function from the previous T, so a hand-written type such as (user: User) => void is narrower than the real setter.

Check
grep -rn "useState(null)" src
grep -rn -E "set[A-Z][A-Za-z]*: *\(" src

Fix

Give useState the full type, and declare the setter in the context type with React's own types and the same T.

Fix
import { useState, type Dispatch, type SetStateAction } from 'react';

type AuthValue = {
  user: User | null;
  setUser: Dispatch<SetStateAction<User | null>>;
};

const [user, setUser] = useState<User | null>(null);

Verify

setUser(realUser), setUser(null) and setUser((prev) => prev) all compile.

Verify
npx tsc --noEmit

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Always write createContext<YourType>(...) with the type argument and never rely on inference from the default.
  • Export one hook per context and keep the null check inside it, so components never touch the raw context object.
  • Write useState<T>(...) with a type argument whenever the initial value is null, an empty array or an empty object.
  • Type-check the whole project on every pull request; a change to the context type then fails in each consumer at the same time.

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 on the same stack: TaxiDex, QistLock, Taskvare.

Next step

Want this handled for good?

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