Skip to content

Commit 70ae8d8

Browse files
committed
fix(send): correctly typing optional payloads
1 parent d45955f commit 70ae8d8

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

playground/app/pages/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ const items = ['notifications', 'chat'] as const
6565
const channels = ref<Array<typeof items[number]>>([])
6666
6767
const { states, status, send, open, close } = useWS<{
68-
notifications: {
68+
notifications?: {
6969
message: string
7070
}
71-
chat: {
71+
chat?: {
7272
user?: string
7373
text: string
7474
}[]

src/runtime/app/composables/ws.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export function useWS<
147147
*/
148148
function send<
149149
Type extends 'subscribe' | 'unsubscribe',
150-
Topic extends keyof T,
150+
Topic extends keyof Required<T>,
151151
>(type: Type, topic: Topic): boolean
152152
/**
153153
* Sends a payload to a specific WebSocket topic.
@@ -167,8 +167,8 @@ export function useWS<
167167
*/
168168
function send<
169169
Type extends 'publish',
170-
Topic extends keyof T,
171-
Payload extends T[Topic] extends Array<infer U> ? U : T[Topic],
170+
Topic extends keyof Required<T>,
171+
Payload extends Required<T>[Topic] extends Array<infer U> ? U : Required<T>[Topic],
172172
>(type: Type, topic: Topic, payload: Payload): boolean
173173
function send<
174174
M extends (

src/runtime/types/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ export interface UseWSReturn<T extends Record<string | AllTopics, any>, D> exten
3838
>(payload: Payload): boolean
3939
send<
4040
Type extends 'subscribe' | 'unsubscribe',
41-
Topic extends keyof T,
41+
Topic extends keyof Required<T>,
4242
>(type: Type, topic: Topic): boolean
4343
send<
4444
Type extends 'publish',
45-
Topic extends keyof T,
46-
Payload extends T[Topic] extends Array<infer U> ? U : T[Topic],
45+
Topic extends keyof Required<T>,
46+
Payload extends Required<T>[Topic] extends Array<infer U> ? U : Required<T>[Topic],
4747
>(type: Type, topic: Topic, payload: Payload): boolean
4848
/**
4949
* Upstream, mainly for internal use.

0 commit comments

Comments
 (0)