microsoft / microsoft/TypeScript
Feature Request: Readonly<T> should remove mutable methods
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
Search Terms
readonly, as const, Readonly,
Suggestion
Readonly to work the way Readonly<T[]> does without having to create something like ReadonlyArray, ReadonlySet, etc...
I would love it if the Readonly<T> mapped type could exclude any method marked as readonly or maybe as cosnt or something similar.
Use Cases
At the moment I have to do this:
class Vector2 {
public constructor(
public x: number,
public x: number,
) {}
public add(other: ReadonlyVector2): Vector2 {
return this.clone().addInPlace(other);
}
public addInPlace(other: ReadonlyVector2): this {
this.x += other.x;
this.y += other.y;
return this;
}
public clone(): Vector2 {
return new Vector2(this.x, this.y);
}
}
interface ReadonlyVector2 {
readonly x: number;
readonly y: number;
add(other: ReadonlyVector2): Vector2;
clone(): Vector2;
}
This gets very boring with a lot of types, also it's error prone. I could forget to add something to the interface, or worse I could accidentally add something that is mutable.
I can currently do method(this: Readonly<this>) {...}, which is helpful, but doesn't stop me calling mutable methods on the type.
Inside a marked method you would be unable to mutate the state of member variables and you would unable to call other unmarked methods.
This would also allow you to remove the special case of Readonly<T[]>. The mutable methods of arrays could be marked with whatever syntax is decided and then Readonly<T[]> would just work like any other Readonly<T>. Currently I don't bother using Readonly<T> since it doesn't really help with anything except C style POD types.
Examples
class Vector2 {
public constructor(
public x: number,
public x: number,
) {}
public readonly add(other: Readonly<Vector2>): Vector2 {
return this.clone().addInPlace(other);
}
public addInPlace(other: Readonly<Vector2>): this {
this.x += other.x;
this.y += other.y;
return this;
}
public const clone(): Vector2 {
return new Vector2(this.x, this.y);
}
}
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. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 d’implémentation, test ou point d’entrée n’est nommé ; commencez par examiner la proposition et ses implications pour le système de types. Le travail serait considéré comme terminé lorsqu’une conception approuvée définirait comment Readonly identifie et supprime les méthodes mutables, notamment la relation avec Readonly<T[]> et les annotations de méthodes proposées.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- 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
- 30/100