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

JSDoc recursive inference not working properly on generic array wrapping #60988

Open
micnic opened this issue Jan 17, 2025 · 0 comments
Open

Comments

@micnic
Copy link
Contributor

micnic commented Jan 17, 2025

πŸ”Ž Search Terms

"jsdoc", "recursive", "inference", "generic"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about everything.

⏯ Playground Link

https://www.typescriptlang.org/play/?filetype=js#code/PQKhFgCgAIWgBALgUwLYAcA2BDF0DaAKtALzTYB2AngLpSzBQDGOAzq9AMrQDeUUASFARIA2AkRV0yXp2SIAPIQB8AX0ENBreQG5+o4Rrjx02AE7ZUs+UrXRtiI41FMA9hVaIzAVyaJXZgAUDgCUvIICiAAWAJasAHQOpPa6guqQguYWVIFhfKICZvLeZhTQFMgA7lyBFdVyiIH48S3RcYnyNPGo2OiBMShWJMoEA2g0IZM60MDA0EWIJR5cSvg0ayMARt6I0MgAHtJ+yAAmK4Qb0ACEN2lQ6VCGMMaDWLgyRMmUtPTGphZWHgNWyqFKOZ7ONweXasACCZmyyWC8jCw3CogWS3KVRqdS48iaLQ6iC6PT6YyGI3wFImUxmc0xpQ4nFWNBG2A4ByOKBO9x0QA

πŸ’» Code

JS with JSDoc typing:

/**
 * @template [T = any]
 */
class S {

  /**
  * @type {Set<T>}
  */
  set;

  /**
  * @param {Set<T>} set
  */
  constructor(set) {
    this.set = set;
  }

  array() {
    return new S(new Set([...this.set].map(item => [item]))); // returns S<T[][]> but expected S<T[]> !!!
  }
}

/**
 * @template [T = any]
 * @param {Set<T>} set
 */
function sArray(set) {
  return new S(new Set([...set].map(item => [item]))); // returns S<T[]> as expected
};

TS version:

class S<T> {

  set: Set<T>;

  constructor(set: Set<T>) {
    this.set = set;
  }

  array() {
    return new S(new Set([...this.set].map(item => [item]))); // returns S<T[]> as expected
  }
}

function sArray<T>(set: Set<T>) {
  return new S(new Set([...set].map(item => [item]))); // returns S<T[]> as expected
};

Link to TS version: https://www.typescriptlang.org/play/?&filetype=ts#code/MYGwhgzhAEDKA8AVAfNA3gWAFDetCApgC4BccxSyA3NrtMAPYB2ERATgK7BENsAUhUuSKUAlOjp4iACwCWEAHSDoAXnzEaWPAF9aW6GDZswATz7jM+vG2Ic2TaEwIB3OHyevYxPgG0F-mXklYgBdBQBbMAAHPlkiAnDVVB84hJDRDKpoAHps6BsiOxY4JB8Q1EhoAgAPKIJuAgATOl0sVuwAMw4mbllmfABBI1NKAWIyLxEUCzoCoscXNw9hX39gojDImNTElWSd9MycvLn7GAREMoqYGrqG5raqIA

πŸ™ Actual behavior

In JSDoc types when a class with generic type T has a method that returns an instance of the same class but with the generic T[] it's return type will be inferred as T[][]. The same code written in plain TS will return the expected result.

πŸ™‚ Expected behavior

It's expected that the JSDoc version of the code has the same output as the TS version for the method that returns an instance of the class with generic wrapped in T[].

Additional information about the issue

No response

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