TheAlgorithms/JavaScript · error · TypeError

Only string arguments are allowed

Error message

Only string arguments are allowed

What it means

Thrown by convertArbitraryBase when any of its three arguments (stringInBaseOne, baseOneCharacterString, baseTwoCharacterString) is not of type 'string'. The guard runs before any spreading/iteration so it is the first possible failure of the function. It exists because the algorithm immediately spreads the character sets with [...str], which silently misbehaves on non-string iterables and would yield confusing downstream errors.

Source

Thrown at Conversions/ArbitraryBase.js:31

/**
 * Converts a string from one base to other. Loses accuracy above the value of `Number.MAX_SAFE_INTEGER`.
 * @param {string} stringInBaseOne String in input base
 * @param {string} baseOneCharacters Character set for the input base
 * @param {string} baseTwoCharacters Character set for the output base
 * @returns {string}
 */
const convertArbitraryBase = (
  stringInBaseOne,
  baseOneCharacterString,
  baseTwoCharacterString
) => {
  if (
    [stringInBaseOne, baseOneCharacterString, baseTwoCharacterString]
      .map((arg) => typeof arg)
      .some((type) => type !== 'string')
  ) {
    throw new TypeError('Only string arguments are allowed')
  }

  const baseOneCharacters = [...baseOneCharacterString]
  const baseTwoCharacters = [...baseTwoCharacterString]

  for (const charactersInBase of [baseOneCharacters, baseTwoCharacters]) {
    if (charactersInBase.length !== new Set(charactersInBase).size) {
      throw new TypeError(
        'Duplicate characters in character set are not allowed'
      )
    }
  }
  const reversedStringOneChars = [...stringInBaseOne].reverse()
  const stringOneBase = baseOneCharacters.length
  let value = 0
  let placeValue = 1
  for (const digit of reversedStringOneChars) {
    const digitNumber = baseOneCharacters.indexOf(digit)

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Wrap each argument in String(...) before calling, or ensure the caller already produces strings.
  2. Switch to convertArbitraryBaseBigIntVersion only if you need arbitrary precision — it still requires strings, it does not accept BigInts.
  3. Add a typeof pre-check in the caller and fail early with a clearer, app-specific message.

Example fix

// before
convertArbitraryBase(255, '0123456789', '01')
// after
convertArbitraryBase(String(255), '0123456789', '01')
Defensive patterns

Strategy: type-guard

Validate before calling

const isAllStrings = (...args) => args.every((a) => typeof a === 'string')
if (!isAllStrings(input, srcAlpha, dstAlpha)) {
  throw new TypeError('All arguments must be strings')
}
convertArbitraryBase(input, srcAlpha, dstAlpha)

Type guard

const isStringTuple = (a, b, c) =>
  [a, b, c].every((x) => typeof x === 'string')

Try / catch

try {
  convertArbitraryBase(input, srcAlpha, dstAlpha)
} catch (e) {
  if (e instanceof TypeError && /Only string arguments/.test(e.message)) {
    // coerce and retry, or surface a user-facing error
  }
  throw e
}

Prevention

When it happens

Trigger: Calling convertArbitraryBase(255, '0123456789', '01') (number literal as first arg), passing null/undefined for a character set, or passing an array like ['a','b'] instead of the string 'ab'. Any one non-string argument is enough because the guard uses .some() over all three typeof checks.

Common situations: Reading input from a form or JSON parsed value that came back as a number; forgetting to stringify a numeric source value; passing a BigInt by mistake into the non-BigInt variant; defaulting a parameter to undefined instead of ''.

Related errors


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