Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/devframe/src/client/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface DevframeScopedClientRpc<NS extends string = string> {

/** Resolve a namespaced shared state. Bare keys resolve within this namespace. */
sharedState: {
<T extends keyof ScopedSharedStates<NS> & string>(key: T, options?: RpcSharedStateGetOptions<ScopedSharedStates<NS>[T]>): Promise<SharedState<ScopedSharedStates<NS>[T]>>
<T extends keyof ScopedSharedStates<NS>>(key: T & string, options?: RpcSharedStateGetOptions<ScopedSharedStates<NS>[T]>): Promise<SharedState<ScopedSharedStates<NS>[T]>>
<T extends Record<string, any> = Record<string, any>>(key: string, options?: RpcSharedStateGetOptions<T>): Promise<SharedState<T>>
}

Expand Down
2 changes: 1 addition & 1 deletion packages/devframe/src/types/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export interface DevframeScopedNodeRpc<NS extends string = string> {
* namespace; pass a fully-qualified key (containing `:`) to opt out.
*/
sharedState: {
<T extends keyof ScopedSharedStates<NS> & string>(key: T, options?: RpcSharedStateGetOptions<ScopedSharedStates<NS>[T]>): Promise<SharedState<ScopedSharedStates<NS>[T]>>
<T extends keyof ScopedSharedStates<NS>>(key: T & string, options?: RpcSharedStateGetOptions<ScopedSharedStates<NS>[T]>): Promise<SharedState<ScopedSharedStates<NS>[T]>>
<T extends Record<string, any> = Record<string, any>>(key: string, options?: RpcSharedStateGetOptions<T>): Promise<SharedState<T>>
}

Expand Down
41 changes: 41 additions & 0 deletions packages/devframe/test/scoped-shared-state.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<SharedState<DevframeServicesState>>()
expectTypeOf(client.sharedState('services')).resolves.toEqualTypeOf<SharedState<DevframeServicesState>>()

// @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<SharedState<MessageState>>()
expectTypeOf(messageClient.sharedState(messageId)).resolves.toEqualTypeOf<SharedState<MessageState>>()

// @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)
})
2 changes: 1 addition & 1 deletion tests/__snapshots__/tsnapi/devframe/client.snapshot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface DevframeScopedClientRpc<NS extends string = string> {
(_: string, ..._: any[]): Promise<any>;
};
sharedState: {
<T extends keyof ScopedSharedStates<NS> & string>(_: T, _?: RpcSharedStateGetOptions<ScopedSharedStates<NS>[T]>): Promise<SharedState<ScopedSharedStates<NS>[T]>>;
<T extends keyof ScopedSharedStates<NS>>(_: T & string, _?: RpcSharedStateGetOptions<ScopedSharedStates<NS>[T]>): Promise<SharedState<ScopedSharedStates<NS>[T]>>;
<T extends Record<string, any> = Record<string, any>>(_: string, _?: RpcSharedStateGetOptions<T>): Promise<SharedState<T>>;
};
streaming: DevframeScopedClientStreamingHost;
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/tsnapi/devframe/index.snapshot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export interface DevframeScopedNodeRpc<NS extends string = string> {
(_: ScopedBroadcastOptions<string, any[]>): Promise<void>;
};
sharedState: {
<T extends keyof ScopedSharedStates<NS> & string>(_: T, _?: RpcSharedStateGetOptions<ScopedSharedStates<NS>[T]>): Promise<SharedState<ScopedSharedStates<NS>[T]>>;
<T extends keyof ScopedSharedStates<NS>>(_: T & string, _?: RpcSharedStateGetOptions<ScopedSharedStates<NS>[T]>): Promise<SharedState<ScopedSharedStates<NS>[T]>>;
<T extends Record<string, any> = Record<string, any>>(_: string, _?: RpcSharedStateGetOptions<T>): Promise<SharedState<T>>;
};
streaming: DevframeScopedStreamingHost;
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/tsnapi/devframe/types.snapshot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export interface DevframeScopedNodeRpc<NS extends string = string> {
(_: ScopedBroadcastOptions<string, any[]>): Promise<void>;
};
sharedState: {
<T extends keyof ScopedSharedStates<NS> & string>(_: T, _?: RpcSharedStateGetOptions<ScopedSharedStates<NS>[T]>): Promise<SharedState<ScopedSharedStates<NS>[T]>>;
<T extends keyof ScopedSharedStates<NS>>(_: T & string, _?: RpcSharedStateGetOptions<ScopedSharedStates<NS>[T]>): Promise<SharedState<ScopedSharedStates<NS>[T]>>;
<T extends Record<string, any> = Record<string, any>>(_: string, _?: RpcSharedStateGetOptions<T>): Promise<SharedState<T>>;
};
streaming: DevframeScopedStreamingHost;
Expand Down
Loading