|
| 1 | +--- |
| 2 | +title: Typing |
| 3 | +meta_description: "Type objects on a channel for type safety and code autocompletion." |
| 4 | +product: liveobjects |
| 5 | +languages: |
| 6 | + - javascript |
| 7 | +--- |
| 8 | + |
| 9 | +blang[javascript]. |
| 10 | + |
| 11 | + If you are using TypeScript in your project, you can leverage LiveObjects' built-in TypeScript support to ensure type safety and enable autocompletion when working with objects on a channel. |
| 12 | + |
| 13 | + h2(#global). Global ObjectsTypes interface |
| 14 | + |
| 15 | + You can type objects on all your channels by defining a global @ObjectsTypes@ interface. If you only want to type the root object for a specific channel, see the "Typing channel.objects.getRoot()":#getroot section below. |
| 16 | + |
| 17 | + Define the @ObjectsTypes@ interface in a type declaration file. You can create a file named @ably.config.d.ts@ in the root of your application: |
| 18 | + |
| 19 | +blang[javascript]. |
| 20 | + |
| 21 | + ```[javascript] |
| 22 | + // file: ably.config.d.ts |
| 23 | + import { LiveCounter, LiveMap } from 'ably'; |
| 24 | + |
| 25 | + // Define dedicated types and export them for reuse in your application |
| 26 | + export type MyCustomRoot = { |
| 27 | + reactions: LiveMap<{ |
| 28 | + hearts: LiveCounter; |
| 29 | + likes: LiveCounter; |
| 30 | + }>; |
| 31 | + }; |
| 32 | + |
| 33 | + declare global { |
| 34 | + export interface ObjectsTypes { |
| 35 | + root: MyCustomRoot; |
| 36 | + } |
| 37 | + } |
| 38 | + ``` |
| 39 | + |
| 40 | +blang[javascript]. |
| 41 | + |
| 42 | + This enables TypeScript to infer the correct types when accessing and mutating LiveObjects: |
| 43 | + |
| 44 | +blang[javascript]. |
| 45 | + |
| 46 | + ```[javascript] |
| 47 | + // LiveMap<{ reactions: LiveMap<{ hearts: LiveCounter; likes: LiveCounter }> }> |
| 48 | + const root = await channel.objects.getRoot(); |
| 49 | + |
| 50 | + // LiveMap<{ hearts: LiveCounter; likes: LiveCounter }> |
| 51 | + const reactions = root.get('reactions'); |
| 52 | + |
| 53 | + // LiveCounter |
| 54 | + const likes = reactions.get('likes'); |
| 55 | + |
| 56 | + reactions.set('hearts', 1); // Error: Argument of type 'number' is not assignable to parameter of type 'LiveCounter'.ts(2345) |
| 57 | + ``` |
| 58 | + |
| 59 | +blang[javascript]. |
| 60 | + |
| 61 | + h2(#getroot). Typing channel.objects.getRoot() |
| 62 | + |
| 63 | + You can pass a type parameter directly to the @channel.objects.getRoot<T>()@ method call to type the root object for a channel explicitly: |
| 64 | + |
| 65 | +blang[javascript]. |
| 66 | + |
| 67 | + ```[javascript] |
| 68 | + // Define types for different root objects |
| 69 | + type ReactionsRoot = { |
| 70 | + hearts: LiveCounter; |
| 71 | + likes: LiveCounter; |
| 72 | + }; |
| 73 | + |
| 74 | + type PollsRoot = { |
| 75 | + currentPoll: LiveMap; |
| 76 | + }; |
| 77 | + |
| 78 | + // LiveMap<{ hearts: LiveCounter; likes: LiveCounter }> |
| 79 | + const reactionsRoot = await reactionsChannel.objects.getRoot<ReactionsRoot>(); |
| 80 | + |
| 81 | + // LiveMap<{ currentPoll: LiveMap }> |
| 82 | + const pollsRoot = await pollsChannel.objects.getRoot<PollsRoot>(); |
| 83 | + ``` |
| 84 | + |
| 85 | +blang[javascript]. |
| 86 | + |
| 87 | + Typing @channel.objects.getRoot<T>()@ is particularly useful when your application uses multiple channels, each with a different objects structure. |
0 commit comments