Last active
October 28, 2025 06:56
-
-
Save cunneen/5d1b0713c98669bba28af7156f40cabb to your computer and use it in GitHub Desktop.
JSDoc: restricting key names and allowable values in VSCode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Defining restrictions on allowable property names is easy in typescript; in JSDoc, not so much. | |
| * | |
| * Here's what I found works with javascript/JSDoc, so far as working fully with IntelliSense in VSCode. | |
| * The secret sauce is: | |
| * | |
| * \@typedef {{[S in LogLevelName]: LogFunction}} LoggerObject | |
| */ | |
| /** | |
| * Valid log level names as a string enum | |
| * @typedef {"error" | "warn" | "success" | "successv" | "info" | "help" | "verbose" | "debug" | "silly"} LogLevelName | |
| */ | |
| /** | |
| * Valid log level numeric values | |
| * @typedef {0 | 1 | 2 | 4 | 5 | 6} LogLevelNumeric | |
| */ | |
| /** | |
| * Map of log level names to numeric values | |
| * @typedef {{[S in LogLevelName]: LogLevelNumeric}} LogLevelMap | |
| */ | |
| /** @type {LogLevelMap} */ | |
| const levels = { | |
| error: 0, | |
| warn: 1, | |
| success: 2, | |
| successv: 2, | |
| info: 2, | |
| help: 2, | |
| verbose: 4, | |
| debug: 5, | |
| silly: 6, | |
| }; | |
| /** | |
| * Generic form of a logging function | |
| * @typedef {function(string, ...any): void} LogFunction | |
| */ | |
| /** | |
| * A type of object that provides log functions for each log level | |
| * @typedef {{[S in LogLevelName]: LogFunction}} LoggerObject | |
| */ | |
| /** | |
| * A logger object type that extends LoggerObject | |
| * @typedef {Object} Logger | |
| * @property {LogLevelName} level | |
| * @property {function(LogLevelName): void} setLevel - set logger level | |
| * @property {function(string): void} setName - set logger name | |
| * @property {function(string): void} setVersion - set logger version | |
| */ | |
| /** @type {Logger & LoggerObject} */ | |
| const logger = { | |
| // track level | |
| level: "debug", | |
| // errors | |
| error: (msg, ...otherArgs) => console.error("error", msg, ...otherArgs), | |
| // .... and so on ... | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To see how VSCode / Codesandbox.io handles the Intellisense, check out this codesandbox :
Here are some screenshots: