Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cunneen/5d1b0713c98669bba28af7156f40cabb to your computer and use it in GitHub Desktop.
Save cunneen/5d1b0713c98669bba28af7156f40cabb to your computer and use it in GitHub Desktop.
JSDoc: restricting key names and allowable values in VSCode
/*
* 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 ...
};
@cunneen
Copy link
Author

cunneen commented Oct 28, 2025

To see how VSCode / Codesandbox.io handles the Intellisense, check out this codesandbox :

Edit JSDoc - How to restrict property names and type values (forked)

Here are some screenshots:

Screenshot 2025-10-28 at 2 48 13 pm Screenshot 2025-10-28 at 2 48 47 pm Screenshot 2025-10-28 at 2 49 42 pm Screenshot 2025-10-28 at 2 49 55 pm Screenshot 2025-10-28 at 2 55 48 pm Screenshot 2025-10-28 at 2 50 42 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment