InterpolatedString
import type { InterpolatedString } from "https://garn.io/ts/v0.0.20/internal/interpolatedString.ts";
Represents a string with T
s interspersed throughout the string.
This is a more safe data structure than joining a TemplateStringsArray
with a T[]
(which is what tag template functions give you) since it
enforces that given N strings, there will always be N-1 interpolations.
Example:
// Given:
myTemplateFn`foo${1}bar${2}baz`
function myTemplateFn(s: TemplateStringsArray, ...i: Array<number>) {
// s is ["foo", "bar", "baz"]
// i is [1, 2]
// but nothing at the type level enforces that s.length - 1 == i.length
// Using `interpolatedStringFromTemplate` we can convert to a safer `InterpolatedString`:
const interpolatedString = interpolatedStringFromTemplate(s, i);
// interpolatedString is { initial: "foo", rest: [[1, "bar"], [2, "baz"]] }
}
type InterpolatedString<T> = {
initial: string;
rest: Array<[T, string]>;
};