TypeScript error

TS18048: A variable that may be undefined, used without a check

  • Error TS18048
  • Reported with strictNullChecks
  • Reported with strict
  • Tested with TypeScript 5.9

error TS18048: 'note' is possibly 'undefined'. is reported when a variable, parameter or property whose type includes undefined is used as if it were always set. It is the named form of TS2532.

In JSDoc, an optional parameter (@param {string} [note]) and an optional property ({ discount?: number }) both include undefined, which is the point of marking them optional: the code using them has to handle the case where they are left out.

At a glance

CodeTS18048
Message'{0}' is possibly 'undefined'.
TypeScript's categoryerror
Listed underNull, undefined and unknown
Turned on by strictNullChecks, strict
In .js files Reported when the file is checked: checkJs in jsconfig.json, or // @ts-check at the top of the file

The message is TypeScript's own text from its diagnostic messages file (Apache-2.0); {0} is filled in by tsc for each error.

What it looks like

catalog.js Error confirmed 5.9.3 TypeScript 5.9.3, run 21 October 2026: catalog.js(10,10): error TS18048: 'product' is possibly 'undefined'. catalog.js(16,10): error TS18048: 'note' is possibly 'undefined'. 6.0.3 TypeScript 6.0.3, run 21 October 2026: the same as 5.9.3. catalog.js(10,10): error TS18048: 'product' is possibly 'undefined'. catalog.js(16,10): error TS18048: 'note' is possibly 'undefined'. 7.0.2 TypeScript 7.0.2, run 21 October 2026: the same as 5.9.3. catalog.js(10,10): error TS18048: 'product' is possibly 'undefined'. catalog.js(16,10): error TS18048: 'note' is possibly 'undefined'. checked 21 October 2026
/** @typedef {{ sku: string, name: string, price: number }} Product */

/** @type {Product[]} */
const products = [{ sku: "TEE-01", name: "Cotton tee", price: 19 }];

/** @param {string} sku */
export function priceOf(sku) {
  const product = products.find((p) => p.sku === sku);
  return product.price;error TS18048: 'product' is possibly 'undefined'.
}

/** @param {string} [note] */
export function giftNote(note) {
  return note.trim();error TS18048: 'note' is possibly 'undefined'.
}
A variable that may be undefined: find() and an optional parameter.
jsconfig.json for this example
{
  "compilerOptions": {
    "checkJs": true,
    "noEmit": true,
    "strict": true,
    "target": "es2022",
    "module": "nodenext",
    "types": []
  },
  "include": ["*.js"]
}

What causes it

  • The variable holds a result that may be undefined (find, Map.get, an optional property).
  • A parameter is marked optional in its @param tag, or has undefined in its type, and is used without a default or a check.
  • strictNullChecks (part of strict) is on.

How to fix it

Check it, or give it a default

if (product === undefined) return 0; narrows the variable for the lines after it. For a parameter, a default value (note = "") removes undefined from its type inside the function while callers may still leave it out.

catalog.js TypeScript 5.9.3 TypeScript 5.9.3, run 21 October 2026: No errors. 6.0.3 TypeScript 6.0.3, run 21 October 2026: the same as 5.9.3. No errors. 7.0.2 TypeScript 7.0.2, run 21 October 2026: the same as 5.9.3. No errors. checked 21 October 2026
/** @typedef {{ sku: string, name: string, price: number }} Product */

/** @type {Product[]} */
const products = [{ sku: "TEE-01", name: "Cotton tee", price: 19 }];

/** @param {string} sku */
export function priceOf(sku) {
  const product = products.find((p) => p.sku === sku);
  if (product === undefined) return 0;
  return product.price;
}

/** @param {string} [note] */
export function giftNote(note = "") {
  return note.trim();
}
A check, and a default value.
jsconfig.json for this example
{
  "compilerOptions": {
    "checkJs": true,
    "noEmit": true,
    "strict": true,
    "target": "es2022",
    "module": "nodenext",
    "types": []
  },
  "include": ["*.js"]
}

Or make it required

If every caller passes the value, the brackets in @param {string} [note] are wrong: remove them, and calls that leave the argument out are reported instead.

Gotchas

  • Truthiness checks (if (note)) also skip the empty string and 0. Compare with undefined when those values are valid.
  • exactOptionalPropertyTypes changes how optional properties treat an explicit undefined, not whether they need a check.

Sources

Last reviewed 20 October 2026.