Skip to content

isString function ​

Checks whether a value is a string primitive.

ts
import { isString } from 'utils';

Signature ​

ts
export function isString(value: unknown): value is string

Parameters ​

NameDescription
valueThe value to check.

Returns ​

true for a string, the empty string included.

Example ​

ts
isString(''); // true
isString(1); // false

Tests ​

Line coverage: 100 %.

  • ✅ accepts strings only

Source ​

src/guard/is-string.ts (on GitHub)

Source code
ts
/**
 * Checks whether a value is a string primitive.
 *
 * @param value - The value to check.
 * @returns `true` for a string, the empty string included.
 * @example
 * isString(''); // true
 * isString(1); // false
 */
export function isString(value: unknown): value is string {
  return typeof value === 'string';
}
Test file
ts
import { isString } from './is-string';

describe(isString, () => {
  it('accepts strings only', () => {
    expect(isString('')).toBe(true);
    expect(isString(1)).toBe(false);
  });
});

Keywords: is, string, guards, function

Generated from the JSDoc of the sources.