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

Implement JSDoc function overloading runtime type validations #42

Open
kungfooman opened this issue Nov 18, 2023 · 0 comments
Open

Implement JSDoc function overloading runtime type validations #42

kungfooman opened this issue Nov 18, 2023 · 0 comments
Assignees

Comments

@kungfooman
Copy link
Owner

There are multiple ways to do it, for example:

/**
 * @type {{
 *   (a: number, b: number): number;
 *   (a: string, b: string): string;
 * }}
 */
const add = function(a, b) {
    return a + b;
}
const addString = add("a", "b");
const addNumber = add(1, 2);
/**
 * @type {{
 *   (input: number): number;
 *   (input: string): string;
 * }}
 */
const double = (input) => {
  if (typeof input === 'number') {
    return input * 2
  }
  return input + input;
}
const doubleString = double("test");
const doubleNumber = double(123);

TS Playground

But it looks very strange and even shows errors in TS Playground... personally I think this would be nice:

/**
 * @typedef {(a: number, b: number) => number} AddNumber
 * @typedef {(a: string, b: string) => string} AddString
 * @type {AddNumber | AddString}
 */
function add(a, b) {
    return a + b;
}

(but that style isn't supported by TS so far)

@kungfooman kungfooman self-assigned this Nov 18, 2023
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

1 participant