TypeScript says a property does not exist on type never
Fix it fast
Most common cause, 1 of 3useState(null) or useRef(null) typed the value as only null
Pass the real type, including null, as the type argument.
const [user, setUser] = useState<User | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const Ctx = createContext<Settings | null>(null);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.
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.
npx tsc --noEmit -p .Then find the line you see in this list and open its fix:
- 1useState(null) or useRef(null) typed the value as only null
The declaration is useState(null), useRef(null) or createContext(null) with no type argument - 2The variable is assigned inside a callback, so the compiler still sees the first value
A 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 - 3Earlier checks already handled every member of the union
The 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.
grep -rnE "use(State|Ref)\(null\)|createContext\(null\)" srcFix
Pass the real type, including null, as the type argument. DOM refs take the element type.
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.
npx tsc --noEmit -p .Confirmed by react.dev
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.
grep -n "let .*| undefined = undefined" src/*.tsFix
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.
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.
npx tsc --noEmit -p .Confirmed by github.com
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.
grep -n "kind ===" src/shapes.tsFix
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.
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.
npx tsc --noEmit -p .Confirmed by typescriptlang.org
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.