{"id":"84991994be26ad76","repo":"sindresorhus/is","slug":"expected-value-which-is-string-received-value-o","errorCode":null,"errorMessage":"Expected value which is `string`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `string`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1896,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('safe integer', value));\n\t}\n}\n\nexport function assertSet<T = unknown>(value: unknown, message?: string): asserts value is Set<T> {\n\tif (!isSet(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Set', value));\n\t}\n}\n\nexport function assertSharedArrayBuffer(value: unknown, message?: string): asserts value is SharedArrayBuffer {\n\tif (!isSharedArrayBuffer(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('SharedArrayBuffer', value));\n\t}\n}\n\nexport function assertString(value: unknown, message?: string): asserts value is string {\n\tif (!isString(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('string', value));\n\t}\n}\n\nexport function assertSymbol(value: unknown, message?: string): asserts value is symbol {\n\tif (!isSymbol(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('symbol', value));\n\t}\n}\n\nexport function assertTruthy<T>(value: T | Falsy, message?: string): asserts value is T {\n\tif (!isTruthy(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('truthy', value));\n\t}\n}\n\nexport function assertTupleLike<T extends Array<TypeGuard<unknown>>>(value: unknown, guards: [...T], message?: string): asserts value is ResolveTypesOfTypeGuardsTuple<T> {\n\tif (!isTupleLike(value, guards)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('tuple-like', value));","sourceCodeStart":1878,"sourceCodeEnd":1914,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1878-L1914","documentation":"Thrown by `assert.string()` (assertString at source/index.ts:1894) when `isString` rejects the value — only primitive strings pass. Numbers, `null`, `undefined`, and even boxed `new String('x')` objects fail. It is one of the most frequently used assertions in @sindresorhus/is for validating function arguments at trust boundaries.","triggerScenarios":"Calling `assert.string(value)` with `undefined` from a missing property or optional parameter, a number (e.g. an ID kept numeric), `null` from an API, or a Buffer that was expected to be decoded.","commonSituations":"Environment variables absent in a deployment (`process.env.X` is `undefined`); numeric values from JSON where the schema changed; Buffers from `fs.readFile` without an encoding argument; template inputs passed through untyped layers.","solutions":["Trace the value to its source and ensure it is set — for env vars, define the variable or fail fast at startup with a named error.","Convert intentionally non-string values explicitly: `String(value)` or `buffer.toString('utf8')`.","For optional values, branch with `is.string(value)` or use a default before asserting."],"exampleFix":"// before\nconst token = process.env.API_TOKEN;\nassert.string(token); // throws when env var unset\n// after\nconst token = process.env.API_TOKEN;\nif (token === undefined) {\n\tthrow new Error('API_TOKEN environment variable is required');\n}\nassert.string(token);","handlingStrategy":"type-guard","validationCode":"if (typeof value !== 'string') {\n  throw new TypeError(`Expected a string, got ${typeof value}`);\n}","typeGuard":"function isString(value: unknown): value is string {\n  return typeof value === 'string';\n}","tryCatchPattern":"try {\n  assert.string(value);\n} catch (error) {\n  if (error instanceof TypeError) {\n    // common culprits: undefined/null from optional fields, numbers, String objects\n  }\n  throw error;\n}","preventionTips":["Check typeof value === 'string' before the call; note new String('x') is an object and fails","Explicitly stringify numbers (String(n)) rather than relying on implicit coercion","Validate optional/nullable fields at the trust boundary so undefined never reaches the assertion"],"tags":["typescript","type-assertion","string","validation","environment-variables"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}