Documentation

type StringConvertible

String type or object that has a toString method.

type StringConvertible =
| string
| {
toString(): string;
};

Description

This type is often used where an argument is expected that can be converted to a string (i.e. using String(...)), hence doesn’t necessarily need to be a string type itself. This includes strings and numbers, but also all objects that have a toString method — notably, object DeferredString objects, the result of fmt().

Examples

// A function that uses a string
function useString(s: StringConvertible) {
  // ... do something with String(s) or use as string
  console.log("Foo: " + s);
}

useString("abc");
useString(123);
useString(fmt("{:.2f}", 123));

Static members

Related