diff --git a/packages/devframe/src/client/scope.ts b/packages/devframe/src/client/scope.ts index 03b09408..cc4a9a58 100644 --- a/packages/devframe/src/client/scope.ts +++ b/packages/devframe/src/client/scope.ts @@ -65,7 +65,7 @@ export interface DevframeScopedClientRpc { /** Resolve a namespaced shared state. Bare keys resolve within this namespace. */ sharedState: { - & string>(key: T, options?: RpcSharedStateGetOptions[T]>): Promise[T]>> + >(key: T & string, options?: RpcSharedStateGetOptions[T]>): Promise[T]>> = Record>(key: string, options?: RpcSharedStateGetOptions): Promise> } diff --git a/packages/devframe/src/types/scope.ts b/packages/devframe/src/types/scope.ts index b539ed7d..19cd8122 100644 --- a/packages/devframe/src/types/scope.ts +++ b/packages/devframe/src/types/scope.ts @@ -187,7 +187,7 @@ export interface DevframeScopedNodeRpc { * namespace; pass a fully-qualified key (containing `:`) to opt out. */ sharedState: { - & string>(key: T, options?: RpcSharedStateGetOptions[T]>): Promise[T]>> + >(key: T & string, options?: RpcSharedStateGetOptions[T]>): Promise[T]>> = Record>(key: string, options?: RpcSharedStateGetOptions): Promise> } diff --git a/packages/devframe/test/scoped-shared-state.test-d.ts b/packages/devframe/test/scoped-shared-state.test-d.ts new file mode 100644 index 00000000..20647691 --- /dev/null +++ b/packages/devframe/test/scoped-shared-state.test-d.ts @@ -0,0 +1,41 @@ +import type { DevframeScopedNodeRpc, DevframeServicesState } from 'devframe' +import type { DevframeScopedClientRpc } from 'devframe/client' +import type { SharedState } from 'devframe/utils/shared-state' +import { expectTypeOf, it } from 'vitest' + +interface MessageState { + text: string +} + +declare module 'devframe' { + interface DevframeRpcSharedStates { + [key: `test-messages:${string}`]: MessageState + } +} + +declare const node: DevframeScopedNodeRpc<'devframe'> +declare const client: DevframeScopedClientRpc<'devframe'> + +it('infers the shared state type from a scoped string key', () => { + expectTypeOf(node.sharedState('services')).resolves.toEqualTypeOf>() + expectTypeOf(client.sharedState('services')).resolves.toEqualTypeOf>() + + // @ts-expect-error Scoped shared state keys must be strings. + node.sharedState(123) + // @ts-expect-error Scoped shared state keys must be strings. + client.sharedState(Symbol('services')) +}) + +declare const messageNode: DevframeScopedNodeRpc<'test-messages'> +declare const messageClient: DevframeScopedClientRpc<'test-messages'> +declare const messageId: string + +it('accepts only strings for a scoped state registered with a pattern key', () => { + expectTypeOf(messageNode.sharedState(messageId)).resolves.toEqualTypeOf>() + expectTypeOf(messageClient.sharedState(messageId)).resolves.toEqualTypeOf>() + + // @ts-expect-error A string index signature must not admit numeric arguments. + messageNode.sharedState(123) + // @ts-expect-error A string index signature must not admit numeric arguments. + messageClient.sharedState(123) +}) diff --git a/tests/__snapshots__/tsnapi/devframe/client.snapshot.d.ts b/tests/__snapshots__/tsnapi/devframe/client.snapshot.d.ts index c1b5817b..c45d39a2 100644 --- a/tests/__snapshots__/tsnapi/devframe/client.snapshot.d.ts +++ b/tests/__snapshots__/tsnapi/devframe/client.snapshot.d.ts @@ -92,7 +92,7 @@ export interface DevframeScopedClientRpc { (_: string, ..._: any[]): Promise; }; sharedState: { - & string>(_: T, _?: RpcSharedStateGetOptions[T]>): Promise[T]>>; + >(_: T & string, _?: RpcSharedStateGetOptions[T]>): Promise[T]>>; = Record>(_: string, _?: RpcSharedStateGetOptions): Promise>; }; streaming: DevframeScopedClientStreamingHost; diff --git a/tests/__snapshots__/tsnapi/devframe/index.snapshot.d.ts b/tests/__snapshots__/tsnapi/devframe/index.snapshot.d.ts index 466986ee..2ad1420f 100644 --- a/tests/__snapshots__/tsnapi/devframe/index.snapshot.d.ts +++ b/tests/__snapshots__/tsnapi/devframe/index.snapshot.d.ts @@ -340,7 +340,7 @@ export interface DevframeScopedNodeRpc { (_: ScopedBroadcastOptions): Promise; }; sharedState: { - & string>(_: T, _?: RpcSharedStateGetOptions[T]>): Promise[T]>>; + >(_: T & string, _?: RpcSharedStateGetOptions[T]>): Promise[T]>>; = Record>(_: string, _?: RpcSharedStateGetOptions): Promise>; }; streaming: DevframeScopedStreamingHost; diff --git a/tests/__snapshots__/tsnapi/devframe/types.snapshot.d.ts b/tests/__snapshots__/tsnapi/devframe/types.snapshot.d.ts index 6dd07e68..327f0838 100644 --- a/tests/__snapshots__/tsnapi/devframe/types.snapshot.d.ts +++ b/tests/__snapshots__/tsnapi/devframe/types.snapshot.d.ts @@ -340,7 +340,7 @@ export interface DevframeScopedNodeRpc { (_: ScopedBroadcastOptions): Promise; }; sharedState: { - & string>(_: T, _?: RpcSharedStateGetOptions[T]>): Promise[T]>>; + >(_: T & string, _?: RpcSharedStateGetOptions[T]>): Promise[T]>>; = Record>(_: string, _?: RpcSharedStateGetOptions): Promise>; }; streaming: DevframeScopedStreamingHost;