Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defineExtensionMessaging should be marked with this: void #82

Open
minht11 opened this issue Nov 21, 2024 · 3 comments
Open

defineExtensionMessaging should be marked with this: void #82

minht11 opened this issue Nov 21, 2024 · 3 comments

Comments

@minht11
Copy link

minht11 commented Nov 21, 2024

Basically the same idea as with https://typescript-eslint.io/rules/unbound-method/ so tooling knows destructoring is safe here.

export function defineExtensionMessaging<
TProtocolMap extends Record<string, any> = Record<string, any>,
>(config?: ExtensionMessagingConfig): ExtensionMessenger<TProtocolMap> {

function defineExtensionMessaging(this: void, condig: Config)
@aklinker1
Copy link
Owner

Just being a static function already means that it's this is void. The lint rule you linked to only mentions bound functions and changing the this context, but that doesn't apply to non-class functions. And it doesn't mention anything about making destructuing safe... Am I misunderstanding what you're asking for?

@minht11
Copy link
Author

minht11 commented Nov 22, 2024

Currently that lint rule fails for me, hence this issue report.
I think I was bit wrong this should not be declared on defineExtensionMessaging but on sendMessage instead

export interface GenericMessenger<
TProtocolMap extends Record<string, any>,
TMessageExtension,
TSendMessageArgs extends any[],
> {
/**
* Send a message to the background or a specific tab if `tabId` is passed. You can call this
* function anywhere in your extension.
*
* If you haven't setup a listener for the sent `type`, an error will be thrown.
*
* @param type The message type being sent. Call `onMessage` with the same type to listen for that message.
* @param data The data to send with the message.
* @param args Different messengers will have additional arguments to configure how a message gets sent.
*/
sendMessage<TType extends keyof TProtocolMap>(
type: TType,
data: GetDataType<TProtocolMap[TType]>,
...args: TSendMessageArgs
): Promise<GetReturnType<TProtocolMap[TType]>>;

Not 100% sure but interface/class when used as a type behaves almost the same, so when it sees GenericMessenger interface which you try destructoring from it thinks this could belong to GenericMessenger and not be void

@aklinker1
Copy link
Owner

Yeah ok, that makes more sense. If you want to open a PR, that'd be great

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

No branches or pull requests

2 participants