@@ -2,6 +2,8 @@ import * as Ably from 'ably';
2
2
import LiveObjects from 'ably/liveobjects' ;
3
3
import { createSandboxAblyAPIKey } from './sandbox' ;
4
4
5
+ // TODO: check we can refer to types exported by LiveObjects plugin.
6
+
5
7
globalThis . testAblyPackage = async function ( ) {
6
8
const key = await createSandboxAblyAPIKey ( { featureFlags : [ 'enableChannelState' ] } ) ;
7
9
@@ -13,9 +15,23 @@ globalThis.testAblyPackage = async function () {
13
15
const root = await liveObjects . getRoot ( ) ;
14
16
15
17
// check root is recognized as LiveMap TypeScript type
16
- root . get ( 'someKey' ) ;
17
18
root . size ( ) ;
18
19
20
+ // check custom user provided typings from ably.config.d.ts are working:
21
+ // keys on a root:
22
+ const aNumber : number = root . get ( 'numberKey' ) ;
23
+ const aString : string = root . get ( 'stringKey' ) ;
24
+ const aBoolean : boolean = root . get ( 'booleanKey' ) ;
25
+ const couldBeUndefined : string | undefined = root . get ( 'couldBeUndefined' ) ;
26
+ // live objects on a root:
27
+ const counter : Ably . LiveCounter | undefined = root . get ( 'counterKey' ) ;
28
+ const map : LiveObjectsTypes [ 'root' ] [ 'mapKey' ] = root . get ( 'mapKey' ) ;
29
+ // check string literal types works
30
+ // need to use nullish coalescing as we didn't actually create any data on the root,
31
+ // so the next calls would fail. we only need to check that TypeScript types work
32
+ const foo : 'bar' = map ?. get ( 'foo' ) ! ;
33
+ const baz : 'qux' = map ?. get ( 'nestedMap' ) ?. get ( 'baz' ) ! ;
34
+
19
35
// check LiveMap subscription callback has correct TypeScript types
20
36
const { unsubscribe } = root . subscribe ( ( { update } ) => {
21
37
switch ( update . someKey ) {
@@ -29,10 +45,8 @@ globalThis.testAblyPackage = async function () {
29
45
} ) ;
30
46
unsubscribe ( ) ;
31
47
32
- // check LiveCounter types also behave as expected
33
- const counter = root . get ( 'randomKey' ) as Ably . LiveCounter | undefined ;
34
- // use nullish coalescing as we didn't actually create a counter object on the root,
35
- // so the next calls would fail. we only need to check that TypeScript types work
48
+ // check LiveCounter type also behaves as expected
49
+ // same deal with nullish coalescing
36
50
counter ?. value ( ) ;
37
51
const counterSubscribeResponse = counter ?. subscribe ( ( { update } ) => {
38
52
const shouldBeANumber : number = update . inc ;
0 commit comments