{"id":"3b2f639f0dce24d0","repo":"sindresorhus/is","slug":"expected-value-which-is-primitive-received-valu","errorCode":null,"errorMessage":"Expected value which is `primitive`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `primitive`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1854,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('plain object', value));\n\t}\n}\n\nexport function assertPositiveInteger(value: unknown, message?: string): asserts value is number {\n\tif (!isPositiveInteger(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('positive integer', value));\n\t}\n}\n\nexport function assertPositiveNumber(value: unknown, message?: string): asserts value is number {\n\tif (!isPositiveNumber(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('positive number', value));\n\t}\n}\n\nexport function assertPrimitive(value: unknown, message?: string): asserts value is Primitive {\n\tif (!isPrimitive(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('primitive', value));\n\t}\n}\n\nexport function assertPromise<T = unknown>(value: unknown, message?: string): asserts value is Promise<T> {\n\tif (!isPromise(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Promise', value));\n\t}\n}\n\nexport function assertPropertyKey(value: unknown, message?: string): asserts value is PropertyKey {\n\tif (!isPropertyKey(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('PropertyKey', value));\n\t}\n}\n\nexport function assertRegExp(value: unknown, message?: string): asserts value is RegExp {\n\tif (!isRegExp(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('RegExp', value));","sourceCodeStart":1836,"sourceCodeEnd":1872,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1836-L1872","documentation":"Thrown by `assert.primitive()` when the value is not a JavaScript primitive — string, number, bigint, boolean, symbol, null, or undefined. Any object, array, function, or class instance fails. Used where a value must be safely comparable by identity, serializable as a scalar, or usable as a simple key/value.","triggerScenarios":"Calling `assert.primitive(value)` with an object, array, function, Date, or boxed primitive (`new String('x')`, `new Number(1)` — boxed wrappers are objects, not primitives).","commonSituations":"Passing a whole object where a scalar ID was expected (e.g. `user` instead of `user.id`); values from libraries that return boxed/wrapped types; template or logging helpers receiving structured data instead of a displayable scalar; cache keys built from objects.","solutions":["Extract the scalar you actually need before asserting, e.g. pass `entity.id` rather than `entity`.","Unbox wrapper objects with `.valueOf()` or by avoiding `new String()`/`new Number()` construction.","If objects should be stringified for this use, do it explicitly (`JSON.stringify`) rather than relying on implicit coercion.","Check the received type in the message — 'Object'/'Array' points directly at which composite leaked in."],"exampleFix":"// before\nassert.primitive(user); // throws: received 'Object'\ncache.set(user, data);\n// after\nassert.primitive(user.id);\ncache.set(user.id, data);","handlingStrategy":"type-guard","validationCode":"if (value !== null && (typeof value === 'object' || typeof value === 'function')) {\n  throw new TypeError(`Expected primitive, got ${typeof value}`);\n}","typeGuard":"function isPrimitive(value: unknown): value is string | number | boolean | bigint | symbol | null | undefined {\n  return value === null || (typeof value !== 'object' && typeof value !== 'function');\n}","tryCatchPattern":"try {\n  ow(value, ow.any(ow.string, ow.number, ow.boolean, ow.null_, ow.undefined));\n} catch (error) {\n  if (error instanceof ArgumentError) {\n    throw new TypeError(`Expected a primitive value: ${error.message}`);\n  }\n  throw error;\n}","preventionTips":["Boxed values (new String('x'), new Number(1)) are objects, not primitives — never use wrapper constructors with new","Unwrap objects to primitives explicitly (String(v), v.valueOf()) before passing","Arrays and functions are not primitives even when they stringify nicely","null IS a primitive despite typeof null === 'object' — use a guard that special-cases null"],"tags":["type-check","assertion","primitive","runtime-validation"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}