Urbit Typescript bindings
Through the magic of typescript, urbit-api-ts maintains a map from marks to
interfaces. It is however the developer's job to keep these updated. The general
form of adding a mark is as follows.
export type Action = 'increment' | 'decrement';
export interface Update {
count: number;
}
declare module 'urbit-api-ts/lib/marks' {
interface Marks {
'ts-demo-action': Action;
'ts-demo-update': Update;
}
}
This will then ensure that subscriptions and pokes have correct type inference
const { bind, poke } = useUrbitApi(window.ship, onUpdate);
poke('ts-demo', 'ts-demo-action', 'increment')
poke('ts-demo', 'ts-demo-value', 'increment');
const onUpdate: UpdateHandler = ({ application, mark, value }) => {
switch(mark) {
case 'ts-demo-update':
const { count } = value;
console.log(value);
case 'ts-demo-action':
const { count } = value;
console.log(value);
}
}