From 54122d365091f899636d80b8e5d75e4e9ea2b3ec Mon Sep 17 00:00:00 2001 From: Sakana <15715093608@163.com> Date: Tue, 24 Nov 2026 15:59:09 +0800 Subject: [PATCH 1/2] fix(devframe): fix scoped shared state types on TypeScript 7 Use the keys of the remapped state registry directly for the Node and client overloads. The registry already produces string keys. TypeScript 7 rejects the extra intersection when checking the published indexed access types. --- packages/devframe/src/client/scope.ts | 2 +- packages/devframe/src/types/scope.ts | 2 +- .../devframe/test/scoped-shared-state.test-d.ts | 17 +++++++++++++++++ .../tsnapi/devframe/client.snapshot.d.ts | 2 +- .../tsnapi/devframe/index.snapshot.d.ts | 2 +- .../tsnapi/devframe/types.snapshot.d.ts | 2 +- 6 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 packages/devframe/test/scoped-shared-state.test-d.ts diff --git a/packages/devframe/src/client/scope.ts b/packages/devframe/src/client/scope.ts index 03b09408b..e64522077 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, 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 b539ed7dc..95093e8df 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, 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 000000000..5491199ce --- /dev/null +++ b/packages/devframe/test/scoped-shared-state.test-d.ts @@ -0,0 +1,17 @@ +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' + +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')) +}) diff --git a/tests/__snapshots__/tsnapi/devframe/client.snapshot.d.ts b/tests/__snapshots__/tsnapi/devframe/client.snapshot.d.ts index c1b5817b1..018446327 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, _?: 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 466986ee2..c60460417 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, _?: 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 6dd07e686..44a6a1504 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, _?: RpcSharedStateGetOptions[T]>): Promise[T]>>; = Record>(_: string, _?: RpcSharedStateGetOptions): Promise>; }; streaming: DevframeScopedStreamingHost; From 58cd0fc9b4a1c85da0c8f89065c490de1ea45538 Mon Sep 17 00:00:00 2001 From: Sakana <15715093608@163.com> Date: Tue, 24 Nov 2026 16:19:57 +0800 Subject: [PATCH 2/2] fix(devframe): require string keys for scoped shared state Keep the type parameter directly constrained by the scoped map so TypeScript 7 can index it. Require a string at the call site because a namespace with a string index signature also admits numeric keys in keyof. --- packages/devframe/src/client/scope.ts | 2 +- packages/devframe/src/types/scope.ts | 2 +- .../test/scoped-shared-state.test-d.ts | 24 +++++++++++++++++++ .../tsnapi/devframe/client.snapshot.d.ts | 2 +- .../tsnapi/devframe/index.snapshot.d.ts | 2 +- .../tsnapi/devframe/types.snapshot.d.ts | 2 +- 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/devframe/src/client/scope.ts b/packages/devframe/src/client/scope.ts index e64522077..cc4a9a586 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: { - >(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 95093e8df..19cd81221 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: { - >(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 index 5491199ce..206476917 100644 --- a/packages/devframe/test/scoped-shared-state.test-d.ts +++ b/packages/devframe/test/scoped-shared-state.test-d.ts @@ -3,6 +3,16 @@ 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'> @@ -15,3 +25,17 @@ it('infers the shared state type from a scoped string key', () => { // @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 018446327..c45d39a2d 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: { - >(_: 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 c60460417..2ad1420f9 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: { - >(_: 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 44a6a1504..327f08385 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: { - >(_: T, _?: RpcSharedStateGetOptions[T]>): Promise[T]>>; + >(_: T & string, _?: RpcSharedStateGetOptions[T]>): Promise[T]>>; = Record>(_: string, _?: RpcSharedStateGetOptions): Promise>; }; streaming: DevframeScopedStreamingHost;