microsoft / microsoft/TypeScript
Allow to explicitly pass type parameters via JSDoc
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Search Terms
jsdoc generics type parameters constraints
Suggestion
It seems like it is not possible via JSDoc to explicitly tell compiler which type parameters to pass to a generic function.
Use Cases
In TypeScript it is possible to explicitly pass type parameters such as mongoose.model<Model, Schema>('modelName', Schema) while I could not find a way to do same with JSDoc.
Examples
mongoose.model has two signatures, first one with one type parameter and the other with two. To make use of the second signature we must pass types explicitly.
export function model<T extends Document>(
name: string,
schema?: Schema,
collection?: string,
skipInit?: boolean
): Model<T>;
export function model<T extends Document, U extends Model<T>>(
name: string,
schema?: Schema,
collection?: string,
skipInit?: boolean
): U;
// Choose second signature in typescript.
const model = mongoose.model<Model, Schema>('modelName', Schema);
// With JSDoc, compiler always chooses first signature and we receive a type mismatch error.
/** @type {Schema & mongoose.Model<Model>} */
const model = mongoose.model('modelName', Schema);
// But something like this would be great.
const model = mongoose.model/**<Model, Schema>*/('modelName', Schema);
My apologies if this is already possible, but I've spend almost a week battling this.
Related: https://github.com/Microsoft/TypeScript-Node-Starter/issues/101
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript / JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. new expression-level syntax)
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Das Issue nennt keine Implementierungsdateien oder Tests. Beginne damit nachzuverfolgen, wie TypeScript generische Aufrufe und JSDoc-Typannotationen verarbeitet, und sieh dir anschließend die Diskussion mit 34 Kommentaren sowie das verknüpfte TypeScript-Node-Starter-Issue an; fertig ist es, wenn JSDoc für überladene generische Funktionen explizit Typparameter angeben kann, ohne das ausgegebene JavaScript zu verändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100