diff --git a/test/package/browser/template/src/ably.config.d.ts b/test/package/browser/template/src/ably.config.d.ts new file mode 100644 index 000000000..1684f5831 --- /dev/null +++ b/test/package/browser/template/src/ably.config.d.ts @@ -0,0 +1,19 @@ +import { LiveCounter, LiveMap } from 'ably'; + +declare global { + export interface LiveObjectsTypes { + root: { + numberKey: number; + stringKey: string; + booleanKey: boolean; + couldBeUndefined?: string; + mapKey?: LiveMap<{ + foo: 'bar'; + nestedMap?: LiveMap<{ + baz: 'qux'; + }>; + }>; + counterKey?: LiveCounter; + }; + } +} diff --git a/test/package/browser/template/src/index-liveobjects.ts b/test/package/browser/template/src/index-liveobjects.ts index f1eb2601b..0f76eaf73 100644 --- a/test/package/browser/template/src/index-liveobjects.ts +++ b/test/package/browser/template/src/index-liveobjects.ts @@ -2,6 +2,8 @@ import * as Ably from 'ably'; import LiveObjects from 'ably/liveobjects'; import { createSandboxAblyAPIKey } from './sandbox'; +// TODO: check we can refer to types exported by LiveObjects plugin. + globalThis.testAblyPackage = async function () { const key = await createSandboxAblyAPIKey({ featureFlags: ['enableChannelState'] }); @@ -13,9 +15,23 @@ globalThis.testAblyPackage = async function () { const root = await liveObjects.getRoot(); // check root is recognized as LiveMap TypeScript type - root.get('someKey'); root.size(); + // check custom user provided typings from ably.config.d.ts are working: + // keys on a root: + const aNumber: number = root.get('numberKey'); + const aString: string = root.get('stringKey'); + const aBoolean: boolean = root.get('booleanKey'); + const couldBeUndefined: string | undefined = root.get('couldBeUndefined'); + // live objects on a root: + const counter: Ably.LiveCounter | undefined = root.get('counterKey'); + const map: LiveObjectsTypes['root']['mapKey'] = root.get('mapKey'); + // check string literal types works + // need to use nullish coalescing as we didn't actually create any data on the root, + // so the next calls would fail. we only need to check that TypeScript types work + const foo: 'bar' = map?.get('foo')!; + const baz: 'qux' = map?.get('nestedMap')?.get('baz')!; + // check LiveMap subscription callback has correct TypeScript types const { unsubscribe } = root.subscribe(({ update }) => { switch (update.someKey) { @@ -29,10 +45,8 @@ globalThis.testAblyPackage = async function () { }); unsubscribe(); - // check LiveCounter types also behave as expected - const counter = root.get('randomKey') as Ably.LiveCounter | undefined; - // use nullish coalescing as we didn't actually create a counter object on the root, - // so the next calls would fail. we only need to check that TypeScript types work + // check LiveCounter type also behaves as expected + // same deal with nullish coalescing counter?.value(); const counterSubscribeResponse = counter?.subscribe(({ update }) => { const shouldBeANumber: number = update.inc; diff --git a/test/package/browser/template/src/tsconfig.json b/test/package/browser/template/src/tsconfig.json index e280baa6d..d950958b4 100644 --- a/test/package/browser/template/src/tsconfig.json +++ b/test/package/browser/template/src/tsconfig.json @@ -1,6 +1,7 @@ { "include": ["**/*.ts", "**/*.tsx"], "compilerOptions": { + "strictNullChecks": true, "resolveJsonModule": true, "esModuleInterop": true, "module": "esnext",