#[js(interface)]
Use #[js(interface)]
to declare:
You can use js(interface)
on any type derived using this crate, such as a
js(value)
or a js(module)
. You can even use it on traits,
see the ts
example.
use ferrosaur::js;
// First, declare a type:
#[js(value)]
struct CowSay;
// Then, declare its APIs:
#[js(interface)]
impl CowSay {
#[js(prop)]
fn moo(&self) -> String {}
}
Example: The To-do List
Let's say you have the following JavaScript:
export const todos = todoList();
function todoList() {
const items = [];
const create = () => {
const todo = new Todo();
items.push(todo);
return todo;
};
return { create };
}
class Todo {
done = false;
}
Expressed in TypeScript declarations, this is:
export declare const todos: TodoList;
interface TodoList {
create: () => Todo;
}
interface Todo {
done: boolean;
}
You can then express this in Rust as:
use ferrosaur::js;
#[js(module("../examples/js/mod.js"))]
struct Module;
#[js(interface)]
impl Module {
#[js(prop)]
fn todos(&self) -> TodoList {}
}
#[js(value)]
struct TodoList;
#[js(interface)]
impl TodoList {
#[js(func)]
fn create(&self) -> Todo {}
}
#[js(value)]
struct Todo;
#[js(interface)]
impl Todo {
#[js(prop(with_setter))]
fn done(&self) -> bool {}
}