-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Labels
Help WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Description
π 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
π» 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
};
π 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
Metadata
Metadata
Assignees
Labels
Help WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases