+
Skip to content

Conversation

iyume
Copy link
Contributor

@iyume iyume commented May 19, 2025

Current behavior on sendMessage:

interface ProtocolMap {
  getId: () => string
  getString: (data: number) => string
  getValue: (data: { val: string }) => number
}

const { sendMessage, onMessage } = defineWindowMessaging<ProtocolMap>({
  namespace: 'xxx',
  logger: console,
})

sendMessage('getId') // An argument for 'data' was not provided.
sendMessage('getId', undefined) // ok

It looks strange to pass undefined again even it's not actually needed. This PR aims to make the data parameter to be automatically optional.

After the changes:

sendMessage('getId') // ok
sendMessage('getId', undefined) // ok
sendMessage('getString') // Argument of type '[]' is not assignable to parameter of type 'never'.
sendMessage('getString', 1) // ok
sendMessage('getValue', { val: '' }) // ok

Copy link
Owner

@aklinker1 aklinker1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I added a few things, like tests and labels for the tuple, but otherwise it looks good. Thanks for figuring this problem out!

Comment on lines 55 to 60
sendMessage<TType extends keyof TProtocolMap>(
type: TType,
...args: GetDataType<TProtocolMap[TType]> extends undefined
? [undefined?, ...TSendMessageArgs]
: never
): Promise<GetReturnType<TProtocolMap[TType]>>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I tried to do this before, but couldn't get it to work. Wonder what's different about your implementation... it's too long ago to remember what I did, but it probably has something to do with the never you're using.

});

it('should send and recieve messages', async () => {
it('should send and receive messages', async () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I also fixed a ton of typos lol. I could not spell "receive" correctly 😆

@aklinker1 aklinker1 merged commit e67e2b5 into aklinker1:main Jun 14, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载