Skip to content

How to read template literals from js? #2996

Answered by Liamolucko
oovm asked this question in Q&A
Discussion options

You must be logged in to vote

Looking at that MDN page, the signature of a tag functions seems to be:

function tag(strings: string[] & { raw: string[] }, ...args: unknown[]): unknown;

The variadic args argument is the tricky part, since exporting functions with variadic arguments isn't supported as of wasm-bindgen 0.2.81. However, support was added in #2954, and will be there in the next release.

So, using a git version of wasm-bindgen, a tag function can be written like this:

use js_sys::Array;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(variadic)]
pub fn tag(strings: Array, args: Array) -> Array {
    // ...
}

#[wasm_bindgen(variadic)] makes the last argument of the function an array of all the arguments passed af…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by oovm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants