TaggedTemplate
Utilities for working with JavaScript tagged templates.
A value of type TaggedTemplate.t<'param, 'output> can be used directly with
backtick syntax, and the compiler emits a real JavaScript tagged template
literal at every call site — across module boundaries, when stored in a value,
or when passed as a function argument:
RES@module("./sql_client.js")
external sql: TaggedTemplate.t<'a, promise<queryResult>> = "sql"
let users = await sql`SELECT * FROM users WHERE id = ${userId}`
make
RES
let make: (
(array<string>, array<'param>) => 'output,
) => t<'param, 'output>make(tag) lifts a plain ReScript function of the tag-function shape
(array<string>, array<'param>) => 'output into a t<'param, 'output>, so it
can itself be used with tagged-template backtick syntax — emitting real
JavaScript tagged-template syntax at every call site.
Examples
REStype params = I(int) | S(string)
let s = TaggedTemplate.make((strings, parameters) => {
Array.reduceWithIndex(parameters, Array.getUnsafe(strings, 0), (acc, param, i) => {
let suffix = Array.getUnsafe(strings, i + 1)
let p = switch param {
| I(i) => Int.toString(i)
| S(s) => s
}
acc ++ p ++ suffix
})
})
let greeting = s`hello ${S("Ada")} you're ${I(36)} years old!`
t
RES
type t<'param, 'output> = taggedTemplate<'param, 'output>Type of a tagged template tag. 'param is the type accepted in ${...}
interpolations, 'output is the type produced by the tag.