Skip to content

liam-fitzgerald/urbit-api-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

// Anywhere in your project
export type Action = 'increment' | 'decrement';
export interface Update {
  count: number;
}


// Maps the Action interface to the 'ts-demo-action' mark
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

// Somewhere in the root component
const { bind, poke } = useUrbitApi(window.ship, onUpdate);

// type checks correctly
poke('ts-demo', 'ts-demo-action', 'increment')

// fails to type check
poke('ts-demo', 'ts-demo-value', 'increment');

// Elsewhere,
const onUpdate: UpdateHandler = ({ application, mark, value }) => {
  switch(mark) {
    case 'ts-demo-update':
      //  typechecks correctly
      const { count } = value;
      console.log(value);
    case 'ts-demo-action':
      // does not typecheck
      const { count } = value;
      console.log(value);
  }
}

About

Typescript bindings for Urbit's eyre interface

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published