microsoft / microsoft/TypeScript
A way to specify class properties using JSDoc
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
Search Terms
JSDoc class properties any key @property
Suggestion
I would like to be able to extend the type of an ES6 class using JSDoc comments, such that TypeScript understands there are other properties that might be added to the object later.
Alternatively, I would like a way to indicate that a given class acts as a container that can contain any possible key.
Use Cases
I have a class that is used like a DTO. It has some properties that are guaranteed to be there, but is also used for a lot of optional properties. Take, for example:
class DTO {
constructor(id) {
/**
* @type {string}
*/
this.id = id;
}
}
TypeScript now recognizes the object type, but whenever I try to use other properties, it complains. Currently, I'm resorting to something like this as a work-around:
class DTO {
constructor(id) {
/**
* @type {string}
*/
this.id = id;
/**
* @type {Object?}
*/
this.otherProperty;
}
}
But it's ugly and verbose, and worse, it includes actual JavaScript code, that serves no purpose other than to provide type information.
Examples
Rather, what I would like to do is something like this (that I would like to be equivalent to the snippet above):
/**
* @property {Object} [otherProperty]
*/
class DTO {
constructor(id) {
/**
* @type {string}
*/
this.id = id;
}
}
Another equivalent alternative could be (but currently doesn't compile because of a "Duplicate identifier" error):
/**
* @typedef {Object} DTO
* @property {string} id
* @property {Object} [otherProperty]
*/
class DTO {
constructor(id) {
this.id = id;
}
}
Or, otherwise, some way to indicate this class can be extended with anything. Which means I would like a way to specify the following TypeScript using JSDoc and I want it to apply to an existing ES6 class (because I do use the class to be able to do instanceof checks):
interface DTO {
id: string,
[key: string]: any,
}
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)
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Aucun fichier, test ou point d’entrée n’est nommé ; commencez par lire les exemples de classes et de propriétés JSDoc ainsi que le comportement demandé pour ES6-class. Le travail est terminé lorsqu’une forme JSDoc convenue permet à TypeScript de reconnaître les propriétés déclarées ou toute propriété supplémentaire arbitraire dans une classe existante sans ajouter de code d’exécution.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100