Maaz Zindani

Loading the index…

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

TypeScript says a property does not exist on type never

Your error

error TS2339: Property 'name' does not exist on type 'never'.2 more lines it can show

Fix it fast

Most common cause, 1 of 3

useState(null) or useRef(null) typed the value as only null

Pass the real type, including null, as the type argument.

Fix
const [user, setUser] = useState<User | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const Ctx = createContext<Settings | null>(null);
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

TS2339 Property 'x' does not exist on type 'never' means narrowing removed every possible type before that line. Usually useState(null) or useRef(null) typed the value as only null, a variable set inside a callback still looks undefined to the compiler, or earlier branches handled every union member. Declare the full type up front. Applies to TypeScript 7.0 and @types/react 19.3.

  • Applies to TypeScript 7.0.2, @types/react 19.3.0, strict mode on
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Hover the variable one line above the error, or ask the compiler, to see what type it had before narrowing. Only null means cause 1; a union that should still hold a value means cause 2; a union whose members were all checked already means cause 3.

Run this first
npx tsc --noEmit -p .

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

  1. 1useState(null) or useRef(null) typed the value as only nullThe declaration is useState(null), useRef(null) or createContext(null) with no type argument
  2. 2The variable is assigned inside a callback, so the compiler still sees the first valueA let declared as Something | undefined = undefined gets its real value inside a callback, event handler or promise, and the error sits on a later x?.prop
  3. 3Earlier checks already handled every member of the unionThe error is in a final else or default branch after if or switch checks on a kind, type or typeof value

What does the error look like?

  • error TS2339: Property 'name' does not exist on type 'never'.
  • Property 'focus' does not exist on type 'never'.
  • Property 'label' does not exist on type 'never'.

Cause 1 of 3

useState(null) or useRef(null) typed the value as only null

You seeThe declaration is useState(null), useRef(null) or createContext(null) with no type argument

Check

TypeScript infers the type from the starting value. Starting with null gives a type of just null, so after user?. or a null check rules null out, nothing is left and the property lookup fails on never. React's TypeScript guide passes the full union as a type argument for exactly this reason.

Check
grep -rnE "use(State|Ref)\(null\)|createContext\(null\)" src

Fix

Pass the real type, including null, as the type argument. DOM refs take the element type.

Fix
const [user, setUser] = useState<User | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const Ctx = createContext<Settings | null>(null);

Verify

The type check passes and user?.name is typed as string | undefined.

Verify
npx tsc --noEmit -p .

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

The variable is assigned inside a callback, so the compiler still sees the first value

You seeA let declared as Something | undefined = undefined gets its real value inside a callback, event handler or promise, and the error sits on a later x?.prop

Check

Control flow analysis narrows a variable to whatever was last assigned in the current function. Calling another function does not undo that narrowing, a trade-off the TypeScript team documents, so the line after the callback still sees undefined; the optional chain removes undefined and leaves never.

Check
grep -n "let .*| undefined = undefined" src/*.ts

Fix

Declare the variable so its starting type is the whole union, not the narrowed undefined. A type assertion on the initial value does that; holding the value in an object property or ref also works.

Fix
let timer = undefined as Timer | undefined;
onReady(() => { timer = makeTimer(); });
console.log(timer?.label);

Verify

The error is gone and timer?.label has the type string | undefined.

Verify
npx tsc --noEmit -p .

Confirmed by github.com

Did this fix it?

Cause 3 of 3

Earlier checks already handled every member of the union

You seeThe error is in a final else or default branch after if or switch checks on a kind, type or typeof value

Check

When type guards strip every option from a union, TypeScript gives the leftover branch the never type. Reading a property there is reported as an error, which usually means a case was forgotten in the type or the branch can never run.

Check
grep -n "kind ===" src/shapes.ts

Fix

If the branch should be reachable, add the missing member to the union. If it should not, make that explicit with an exhaustive check that assigns the value to never instead of reading its properties.

Fix
default: {
  const unreachable: never = shape;
  throw new Error(`Unhandled shape: ${JSON.stringify(unreachable)}`);
}

Verify

tsc passes, and adding a new member to the union later makes the default branch fail to compile.

Verify
npx tsc --noEmit -p .

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Give every useState, useRef and createContext that starts as null an explicit type argument.
  • Make a type-only compile step (tsc with noEmit) a required check on pull requests, so an edit that empties a union is caught in review.
  • Use exhaustive switch statements with a never check on discriminated unions.

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.