TheAlgorithms/JavaScript · error · TypeError

The ${paramName} should be type Number

Error message

The ${paramName} should be type Number

What it means

Thrown by the internal validateNumericParam helper (Area.js:166) as a TypeError when an argument passed to any area/surface-area function is not of type 'number'. The helper is shared by surfaceAreaCube, surfaceAreaSphere, areaRectangle, areaSquare, areaTriangle, areaParallelogram, areaTrapezium, areaCircle, areaRhombus and others. The library uses strict typeof checking, so string-encoded numbers like '5' are rejected. paramName in the message reflects the human-readable label passed by each caller (e.g. 'diagonal one').

Source

Thrown at Maths/Area.js:167

/**
 * @function areaRhombus
 * @description Calculate the area of a rhombus.
 * @param {Integer} diagonal1 - Integer
 * @param {Integer} diagonal2 - Integer
 * @return {Integer} - (1 / 2) * diagonal1 * diagonal2
 * @see [areaRhombus](https://en.wikipedia.org/wiki/Rhombus)
 * @example areaRhombus(12, 10) = 60
 */
const areaRhombus = (diagonal1, diagonal2) => {
  validateNumericParam(diagonal1, 'diagonal one')
  validateNumericParam(diagonal2, 'diagonal two')
  return (1 / 2) * diagonal1 * diagonal2
}

const validateNumericParam = (param, paramName = 'param') => {
  if (typeof param !== 'number') {
    throw new TypeError('The ' + paramName + ' should be type Number')
  } else if (param < 0) {
    throw new Error('The ' + paramName + ' only accepts non-negative values')
  }
}

export {
  surfaceAreaCube,
  surfaceAreaSphere,
  areaRectangle,
  areaSquare,
  areaTriangle,
  areaParallelogram,
  areaTrapezium,
  areaCircle,
  areaRhombus,
  areaTriangleWithAllThreeSides
}

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Coerce inputs explicitly with Number() or parseFloat() before calling, and check for NaN afterwards.
  2. If reading from HTML inputs, use the valueAsNumber property instead of value to get a number directly.
  3. Audit the call site for undefined arising from optional/destructured parameters with no default.
  4. Add a type guard or use TypeScript so non-number arguments are caught at compile time.

Example fix

// before
const a = areaRhombus(inputEl.value, otherInput) // value is a string

// after
const d1 = Number(inputEl.value)
const d2 = Number(otherInput)
if (Number.isNaN(d1) || Number.isNaN(d2)) throw new TypeError('non-numeric input')
const a = areaRhombus(d1, d2)
Defensive patterns

Strategy: type-guard

Validate before calling

const asNumber = (v) => (typeof v === 'number' && !Number.isNaN(v)) ? v : NaN
const d1 = asNumber(diagonal1)
const d2 = asNumber(diagonal2)
if (Number.isNaN(d1) || Number.isNaN(d2)) throw new TypeError('non-numeric input')
const a = areaRhombus(d1, d2)

Type guard

const isNumber = (v) => typeof v === 'number' && !Number.isNaN(v)

Try / catch

try {
  result = areaRhombus(d1, d2)
} catch (e) {
  if (e instanceof TypeError && /should be type Number/.test(e.message)) {
    // handle non-numeric input at boundary
  } else throw e
}

Prevention

When it happens

Trigger: Call areaRhombus('12', 10) with string arguments; pass undefined when an optional parameter is omitted (e.g. areaRectangle(5) where height is undefined); pass null from a JSON source where a field was absent; pass a boolean or object accidentally.

Common situations: Values read from DOM inputs (always strings) or URL query params fed directly to the function; data from JSON.parse where numbers were quoted as strings in the source; a refactored function signature where a previously defaulted param is now required and callers were not updated.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/de44b2247f8a99a5. Report an issue: GitHub.