Skip to main content
Version: 5.0

Defining your Resource types

Typing your data can be very useful and is highly recommended when using this library. One of the main benefits is to enforce immutability, as this brings certain assurances to the correctness of your application as well as enables certain performance enhancements with React.

Here we have an example Resource for a User typed in typescript.

import { Resource } from '@rest-hooks/rest';

export interface Address {
readonly street: string;
readonly suite: string;
readonly city: string;
readonly zipcode: string;
readonly geo: {
readonly lat: string;
readonly lng: string;

export type Status = 'pending' | 'rejected' | 'accepted';

export class UserResource extends Resource {
readonly id: number | undefined = undefined;
readonly name: string = '';
readonly username: string = '';
readonly email: string = '';
readonly phone: string = '';
readonly website: string = '';
readonly address: Address | null = null;
readonly status: Status = 'pending';

pk() {
static urlRoot = '';


  • Define all properties you expect to see
  • Mark them as readonly to enforce immutability
  • Give each property as specific and descriptive a type as possible
  • Initialize the property with a sensible default
    • This will simplify handling so we don't have to deal with undefineds