microsoft / microsoft/TypeScript
Immutable-By-Default Flags
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
Search Terms
readonly, default, immutable
Suggestion
Adding flags under the strict umbrella to default to immutability in different cases, and a new mutable keyword.
Use Cases
I'm creating this issue as a parent issue to track a couple of issues that already exist for specific cases:
- Class methods: https://github.com/microsoft/TypeScript/issues/22315, https://github.com/microsoft/TypeScript/issues/22315#issuecomment-400708470 in particular
- Arrays: https://github.com/microsoft/TypeScript/issues/32467
- Interfaces: No issue yet, left a comment in https://github.com/microsoft/TypeScript/issues/32467#issuecomment-519333922
- https://github.com/microsoft/TypeScript/issues/21152
Off of the top of my head, these flags would have some direct advantages:
- They clearly state the intention of the whole project in regards to mutability
- It's very common for new members of teams to forget to add the
readonlykeyword or useT[]rather thanReadonlyArray<T>accidentally. Defaulting to immutability would help prevent these accidents, and amutablekeyword would be easy to find bytslintor even a simplegrep, to automatically request review in PRs or leave an automated commentthis PR introduces mutation.
Examples
Examples should probably live in the children GitHub issues, but I'm copying here my comment for a quick example:
interface T {
n: number // immutable
mutable s: string // mutable
}
const o: T = {
n: 42,
s: 'hello world',
}
o.n = 43 // error
o.s = '👋🌎' // ok
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.
Backwards Compatibility
@AnyhowStep made an interesting comment on this issue.
Basically this feature wouldn't be any problem for applications, but it could be problematic for libraries, as the emitted .d.ts may be imported from a library/application that isn't using this flag or is using an older TS version.
Possible solutions:
- A pre-processor directive (https://github.com/microsoft/TypeScript/issues/32758#issuecomment-569369439)
- Always emitting
d.tswithreadonly, nevermutable(possibly behind another flag?) - downlevel-dts
Records and Tuples
The Record and Tuple proposal has reached stage 2, so it may be arriving to TS soon-ish. It seems closely related to this issue, but I don't think it fully addresses it.
readonly interface
The possibility of using the readonly keyword for an entire interface has been suggested in A cheaper, easier to implement middle ground could be https://github.com/microsoft/TypeScript/issues/21152.
readonly interface State {
prop1: string;
prop2: string;
...
prop22: string;
}
This would probably be a much cheaper and less disruptive feature to implement, and could be a great starting point.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza por las issues secundarias enlazadas para métodos de clase, arrays, interfaces e interfaces readonly, y después revisa las preocupaciones de compatibilidad de la issue principal. No se indican archivos de implementación ni tests; el trabajo solo estará completo cuando se hayan acordado el alcance, la sintaxis y el comportamiento de los archivos de declaración.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 15/100