Type/Since
1.38.0isString
Checks whether a value is a string primitive.
isString(value):value is stringBehavior
The function checks the value using typeof and returns true only when JavaScript classifies the value as the primitive string type.
Primitive Strings
String literals and values created as primitive strings return true. Objects such as new String("hello") do not return true because their typeof result is object.
Type Guard
The return type is value is string, allowing TypeScript to narrow the value to string inside a conditional branch.
Parameters
valueunknownrequiredThe value to check.
Returns
value is stringReturns true when the value has the JavaScript string type; otherwise, returns false.
Examples
01
Basic usage
Check whether different values are strings.
TypeScript
import { isString } from "@cyref/js";
isString("hello");
// true
isString("");
// true
isString("123");
// true
isString(123);
// false
isString(null);
// false
isString(undefined);
// false02
Type narrowing
isString acts as a TypeScript type guard and narrows the value to string when it returns true.
TypeScript
const value: string | number = "hello";
if (isString(value)) {
console.log(value.toUpperCase());
}Built with@cyref/js·Official documentation