{"record":{"id":"3b88d4e8d556bdc6","repo":"cheeriojs/cheerio","slug":"unexpected-type-of-selector","errorCode":null,"errorMessage":"Unexpected type of selector","messagePattern":"Unexpected type of selector","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/load.ts","lineNumber":225,"sourceCode":"        typeof selector === 'string' && isHtml(selector)\n          ? // $(<html>)\n            parse(selector, options, false, null).children\n          : isNode(selector)\n            ? // $(dom)\n              [selector]\n            : Array.isArray(selector)\n              ? // $([dom])\n                selector\n              : undefined;\n\n      const instance = new LoadedCheerio(elements, rootInstance, options);\n\n      if (elements) {\n        return instance as Cheerio<Result>;\n      }\n\n      if (typeof selector !== 'string') {\n        throw new TypeError('Unexpected type of selector');\n      }\n\n      // We know that our selector is a string now.\n      let search = selector;\n\n      const searchContext: Cheerio<AnyNode> | undefined = context\n        ? // If we don't have a context, maybe we have a root, from loading\n          typeof context === 'string'\n          ? isHtml(context)\n            ? // $('li', '<ul>...</ul>')\n              new LoadedCheerio<Document>(\n                [parse(context, options, false, null)],\n                rootInstance,\n                options,\n              )\n            : // $('li', 'ul')\n              ((search = `${context} ${search}` as S), rootInstance)\n          : isCheerio<AnyNode>(context)","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/cheeriojs/cheerio/blob/a1be131f9b0cef323ef0d65c5babda561413ac7d/src/load.ts#L207-L243","documentation":"When the cheerio function is called with selector-like arguments (no existing Cheerio/element instance), the selector must be a string. Passing any other type (number, object, null, function) as the selector throws this TypeError.","triggerScenarios":"Calling cheerio(123), cheerio({}), cheerio(null) with a context/elements argument combination that routes into the selector branch, or cheerio(someVar) where someVar is not a string, element, or Cheerio instance.","commonSituations":"Passing a parsed JSON object or a DOM-like object that isn't a recognized node; passing numbers from user input; variables that are undefined due to typos or unresolved imports; mixing up argument order.","solutions":["Ensure the selector is a string: cheerio(String(sel)) or validate before calling","If selecting by node, pass an actual parsed node or Cheerio collection, not a plain object","Check the variable is defined and log its type before invoking cheerio()"],"exampleFix":"// before\nconst $ = cheerio(selectorFromInput); // may be a number/object\n// after\nif (typeof selectorFromInput !== 'string') {\n  throw new Error(`Invalid selector: ${typeof selectorFromInput}`);\n}\nconst $ = cheerio(selectorFromInput);","handlingStrategy":"type-guard","validationCode":"if (typeof selector !== 'string' && !isNodeLike(selector)) {\n  throw new Error(`Invalid selector type: ${typeof selector}`);\n}\nconst $ = cheerio(selector);","typeGuard":"function isSelector(sel: unknown): sel is string {\n  return typeof sel === 'string';\n}\n// use: if (isSelector(sel)) cheerio(sel);","tryCatchPattern":"try { const $ = cheerio(sel); } catch (e) { if (e instanceof TypeError && /Unexpected type of selector/.test(e.message)) { console.warn('bad selector', sel); return; } throw e; }","preventionTips":["Validate user-supplied selectors are strings","Pass parsed nodes/Cheerio collections, not plain objects","Check variables are defined before calling cheerio()"],"tags":["cheerio","selector","typeerror","initialization"],"backgroundTag":"invalid-selector-type","analyzedSha":"a1be131f9b0cef323ef0d65c5babda561413ac7d","analyzedAt":"2026-08-28T13:05:26.741Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}