Types
interface Register
Declares your app's data and room types, so the calls of
tinylib use them. data maps each key, or key template, to its value's type. rooms maps each kind to the type its view returns and the type of the events its rules emit.Declare it in a file with an import or export, such as the import type line below. In a file with neither,
declare module 'tinylib-sdk' replaces the package's types instead of adding to them.
tinylib-env.d.ts
import type {} from 'tinylib-sdk'
type GameView = { board: string[]; turn: 'x' | 'o'; winner: 'x' | 'o' | 'draw' | null; you: 'x' | 'o' | null }
declare module 'tinylib-sdk' {
interface Register {
data: { best: number; [key: `entry/${string}`]: string }
rooms: { game: { view: GameView; event: { sound: string } } }
}
}
app.ts
const best = tinylib.data.get('best') // number | undefined
tinylib.rooms.follow<'game'>(id, ({ view }) => {
if (view) drawBoard(view.board) // view is a GameView
})
type RoomView<K extends string> = RoomTypes<K> extends { view: infer V } ? V : unknown
type RoomTypes<K extends string> = K extends keyof RoomShape ? RoomShape[K] : unknown
type RoomShape = Register extends { rooms: infer R } ? R : Record<string, unknown>
The type of a member's view in rooms of kind
K, as rooms in Register declares it, or unknown when it doesn't.type RoomEvent<K extends string> = RoomTypes<K> extends { event: infer E } ? E : unknown
type RoomTypes<K extends string> = K extends keyof RoomShape ? RoomShape[K] : unknown
type RoomShape = Register extends { rooms: infer R } ? R : Record<string, unknown>
The type of the events the rules emit in rooms of kind
K, as rooms in Register declares it, or unknown when it doesn't.interface Member
A user as your app sees them: in
ctx.members in the rules, and in tinylib.friends.list and an invitation's from in your client.Properties
- idstring
- The same in every room of your app.
- namestring
- Names change and aren't unique, so keep
idin state, notname. - guestboolean
- True for a guest: a user without a Tinylib account, known only on the device they use. A guest has data and acts in rooms like anyone, and gets no notifications or invitations.
type Audience = 'me' | 'friends'
Who can read a value:
'me', the user alone, or 'friends', the user and their friends who have the app.