typed-string-interpolation
Version:
String interpolation with correct return type based on passed variable substitutions
24 lines (21 loc) • 1.02 kB
TypeScript
type StringInterpolationOptions<Raw extends boolean | undefined> = {
pattern?: RegExp;
sanity?: boolean;
raw?: Raw;
};
type StringInterpolationReturn<VariableValue extends any, OptionRaw> = Exclude<VariableValue, string | number> extends never ? Extract<OptionRaw, boolean> extends true ? (string | VariableValue)[] : string : (string | VariableValue)[];
/**
* Takes in a string containing variables and an object containing variables for interpolation. Accepts options.
*
* @example
*
* stringInterpolation("You have {{n}} messages", {
n: 3,
})
stringInterpolation("You have {{n}} messages from {{person}}", {
n: <strong>3</strong>,
person: "John",
})
*/
declare function stringInterpolation<VariableValue extends any, OptionRaw extends boolean | undefined>(string: string, variables: Record<PropertyKey, VariableValue>, { pattern, sanity, raw, }?: StringInterpolationOptions<OptionRaw>): StringInterpolationReturn<VariableValue, OptionRaw>;
export { stringInterpolation };