-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Issue
Argument of type 'string' is not assignable to parameter of type '{ [x: string]: string; }'.ts(2345)
for BrowserCommunication.addListener

if you define the fake "enum" like that:
/**
* An object of all available communication types.
*
* @public
* @const
* @type {Object.<string, string>}
*/
Now the issue is wrong JSDOC actually.
See #file:BrowserCommunication.js * @param {COMMUNICATION_MESSAGE_TYPE} messageType type of message
is wrong JSDOC.
If you see #file:BrowserCommunicationTypes.js you see it is an object of strings, but i should just be used more or less like an enum (which JS itself does not have), so you provide a specific value, but cannot provide any arbotrary string.
Tries
I already chatted with Copilot a little and tried some things and the real (but confirmed working) solution for this was to define a @typedef
then the keys are enforced. However, that is of course ugly and code duplication:
/**
* An object of all available communication types.
*
* @public
* @constant
* @typedef {"omnibarToggle"|"autocorrectBackground"|"autocorrectContent"|"contextMenu"|"insert"} CommunicationMessageType
*/
export const COMMUNICATION_MESSAGE_TYPE = Object.freeze({
OMNIBAR_TOGGLE: "omnibarToggle",
AUTOCORRECT_BACKGROUND: "autocorrectBackground",
AUTOCORRECT_CONTENT: "autocorrectContent",
CONTEXT_MENU: "contextMenu",
INSERT: "insert"
});
And then have this in BrowserCommunication.js
(note it needs the import, as the AI missed that of course I otherwise cannot import "types"):
* @param {import("../data/BrowserCommunicationTypes.js").CommunicationMessageType} messageType type of message to receive
This basically would work and TS catches the error in VSCode:

Full chat:
https://gist.github.com/rugk/d9009a50e6a0f7085af390c3b6d4fe57