TS-Scribe - v0.6.2
    Preparing search index...

    Function interpolateString

    • Interpolates a template string by replacing {key} placeholders with values from a data object or array.

      Named placeholders resolve keys against a plain object, with support for dot-notation paths to access nested properties. Positional placeholders ({0}, {1}, …) resolve numeric indices against an array.

      Type Parameters

      • T extends string

        The template string literal type (enables key inference).

      Parameters

      • template: T | undefined

        The template string containing placeholders.

      • data: InterpolateData<T>

        Values to substitute into the template.

      • Optionaloptions: InterpolateOptions

        Optional configuration.

      Returns string

      The interpolated string.

      If open or close delimiters are empty strings.

      If strict is true and a placeholder key is not found in data.

      // Named placeholder
      interpolateString('Hello, {name}!', { name: 'Alice' });
      // Returns "Hello, Alice!"
      // Nested dot-notation path
      interpolateString('Welcome, {user.profile.displayName}.', {
      user: { profile: { displayName: 'Bob' } },
      });
      // Returns "Welcome, Bob."
      // Positional (array) placeholders
      interpolateString('{0} + {1} = {2}', [1, 2, 3]);
      // Returns "1 + 2 = 3"
      // Custom delimiters
      interpolateString('Hello, {{name}}!' as string, { name: 'Carol' }, { open: '{{', close: '}}' });
      // Returns "Hello, Carol!"
      // Fallback for missing keys
      interpolateString('Hi {firstName} {lastName}!', { firstName: 'Dave' }, { fallback: '?' });
      // Returns "Hi Dave ?!"
      // Fallback function
      interpolateString('Hi {firstName} {lastName}!', { firstName: 'Eve' }, {
      fallback: (key) => `<missing:${key}>`,
      });
      // Returns "Hi Eve <missing:lastName>!"
      // Strict mode – throws on missing keys
      interpolateString('Hi {name}!' as string, {}, { strict: true });
      // Throws: RangeError: Missing interpolation key: "name"
      // Value transformer
      interpolateString('Today is {date}.', { date: new Date('2026-01-01') }, {
      transform: (value) => (value as Date).toLocaleDateString('en-US'),
      });
      // Returns "Today is 1/1/2026."