React createContext in TypeScript: errors that come from the default value
Fix it fast
Most common cause, 1 of 4createContext is called without an argument
Pass null and put null in the type, so the type tells the truth about the default.
import { createContext } from 'react';
type AuthValue = { user: User | null; signOut: () => void };
export const AuthContext = createContext<AuthValue | null>(null);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.
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.
npx tsc --noEmit
grep -rn "createContext" srcThen find the line you see in this list and open its fix:
- 1createContext is called without an argument
Expected 1 arguments, but got 0. on the createContext line - 2A null default makes every consumer see a possible null
Property 'user' does not exist on type 'AuthValue | null'. or 'auth' is possibly 'null'. in components that call useContext - 3The context type was inferred from a default that is too narrow
The provider's value prop fails with a message that names a type built from your default, for example a property typed null - 4The state and setter behind the provider have types that are too narrow
Argument 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.
grep -rn -E "createContext(<[^>]*>)?\(\)" srcFix
Pass null and put null in the type, so the type tells the truth about the default.
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.
npx tsc --noEmitConfirmed by raw.githubusercontent.com
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.
grep -rn "useContext(AuthContext)" srcFix
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.
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.
npx tsc --noEmitConfirmed by react.dev
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.
grep -rn -B1 -A3 "createContext(" srcFix
Name the type and pass it as the generic argument, so the default has to fit the type and not the other way round.
// 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.
npx tsc --noEmitConfirmed by react.dev
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.
grep -rn "useState(null)" src
grep -rn -E "set[A-Z][A-Za-z]*: *\(" srcFix
Give useState the full type, and declare the setter in the context type with React's own types and the same T.
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.
npx tsc --noEmitConfirmed by raw.githubusercontent.com
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.