{"record":{"id":"cf93dd1795b1e7db","repo":"TheAlgorithms/JavaScript","slug":"only-string-arguments-are-allowed","errorCode":null,"errorMessage":"Only string arguments are allowed","messagePattern":"Only string arguments are allowed","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Conversions/ArbitraryBase.js","lineNumber":31,"sourceCode":"\n/**\n * Converts a string from one base to other. Loses accuracy above the value of `Number.MAX_SAFE_INTEGER`.\n * @param {string} stringInBaseOne String in input base\n * @param {string} baseOneCharacters Character set for the input base\n * @param {string} baseTwoCharacters Character set for the output base\n * @returns {string}\n */\nconst convertArbitraryBase = (\n  stringInBaseOne,\n  baseOneCharacterString,\n  baseTwoCharacterString\n) => {\n  if (\n    [stringInBaseOne, baseOneCharacterString, baseTwoCharacterString]\n      .map((arg) => typeof arg)\n      .some((type) => type !== 'string')\n  ) {\n    throw new TypeError('Only string arguments are allowed')\n  }\n\n  const baseOneCharacters = [...baseOneCharacterString]\n  const baseTwoCharacters = [...baseTwoCharacterString]\n\n  for (const charactersInBase of [baseOneCharacters, baseTwoCharacters]) {\n    if (charactersInBase.length !== new Set(charactersInBase).size) {\n      throw new TypeError(\n        'Duplicate characters in character set are not allowed'\n      )\n    }\n  }\n  const reversedStringOneChars = [...stringInBaseOne].reverse()\n  const stringOneBase = baseOneCharacters.length\n  let value = 0\n  let placeValue = 1\n  for (const digit of reversedStringOneChars) {\n    const digitNumber = baseOneCharacters.indexOf(digit)","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/ArbitraryBase.js#L13-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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 ''.","solutions":["Wrap each argument in String(...) before calling, or ensure the caller already produces strings.","Switch to convertArbitraryBaseBigIntVersion only if you need arbitrary precision — it still requires strings, it does not accept BigInts.","Add a typeof pre-check in the caller and fail early with a clearer, app-specific message."],"exampleFix":"// before\nconvertArbitraryBase(255, '0123456789', '01')\n// after\nconvertArbitraryBase(String(255), '0123456789', '01')","handlingStrategy":"type-guard","validationCode":"const isAllStrings = (...args) => args.every((a) => typeof a === 'string')\nif (!isAllStrings(input, srcAlpha, dstAlpha)) {\n  throw new TypeError('All arguments must be strings')\n}\nconvertArbitraryBase(input, srcAlpha, dstAlpha)","typeGuard":"const isStringTuple = (a, b, c) =>\n  [a, b, c].every((x) => typeof x === 'string')","tryCatchPattern":"try {\n  convertArbitraryBase(input, srcAlpha, dstAlpha)\n} catch (e) {\n  if (e instanceof TypeError && /Only string arguments/.test(e.message)) {\n    // coerce and retry, or surface a user-facing error\n  }\n  throw e\n}","preventionTips":["Always stringify numeric inputs with String(...) before passing.","Keep character-set alphabets as string constants, never construct them as arrays.","Add a typeof pre-check in boundary code (form handlers, API layers)."],"tags":["type-validation","string","base-conversion","typeof","conversions"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}