tsconfig - Is there a typescript compiler option that prevents implicit widening from readonly properties to readwrite? - Stack
- c - Solaris 10 make Error code 1 Fatal Error when trying to build python 2.7.16 - Stack Overflow 推荐度:
- javascript - How to dismiss a phonegap notification programmatically - Stack Overflow 推荐度:
- javascript - Get the JSON objects that are not present in another array - Stack Overflow 推荐度:
- javascript - VS 2015 Angular 2 import modules cannot be resolved - Stack Overflow 推荐度:
- javascript - Type 'undefined' is not assignable to type 'menuItemProps[]' - Stack Overflow 推荐度:
- 相关推荐
By default, typescript treats readonly
object properties as satisfying standard read/write properties during type checking. This happens without type assertions or any other trickery. This is a surprising behavior, and can lead to runtime problems. Is there any way to turn this off using a compiler option? Am I doing this wrong somehow?
Here's an example of what I'm talking about.
type O = { p: number; };
const r: O = { get p() { return 2; } };
// Passes type checking, but fails at run time.
// Uncaught TypeError: setting getter-only property "p"
r.p = 3;
By default, typescript treats readonly
object properties as satisfying standard read/write properties during type checking. This happens without type assertions or any other trickery. This is a surprising behavior, and can lead to runtime problems. Is there any way to turn this off using a compiler option? Am I doing this wrong somehow?
Here's an example of what I'm talking about.
type O = { p: number; };
const r: O = { get p() { return 2; } };
// Passes type checking, but fails at run time.
// Uncaught TypeError: setting getter-only property "p"
r.p = 3;
Share
Improve this question
asked yesterday
recursiverecursive
86k36 gold badges154 silver badges245 bronze badges
1
- Yes, as disappointing as it is, it is a full answer. Thanks. – recursive Commented yesterday
1 Answer
Reset to default 1This is a longstanding known soundness issue, tracked at microsoft/TypeScript#13347. Currently a readonly
property will prevent you from writing to it directly, but it is still considered assignable both to and from a non-readonly
property. This assignability was made so that the feature wouldn't be a huge breaking change for existing code. It's not completely useless like this, but it's surprising enough that another issue about the same topic, microsoft/TypeScript#13002, was originally given the title "readonly modifiers are a joke".
As of TypeScript 5.7 there is no compiler option to address this, in any released version of the language. However there is an --enforceReadonly
flag implementation at the pull request microsoft/TypeScript#58296, written by the TypeScript language architect. So there's a good chance this will become part of the language at some point in the near-ish future. In the design meeting notes at microsoft/TypeScript#59406 and the TypeScript 5.7 iteration plan at microsoft/TypeScript#58296 it looks like they were considering implementing this with TypeScript 5.7, but it doesn't seem to have happened. I don't see an iteration plan for TypeScript 5.8, so I'm not sure if it will be part of that version either. Maybe TypeScript 5.9? Certainly if it does get released, it will still be a fairly big breaking change for anyone who enables it, which is why it's --enforceReadonly
and not --strictReadonly
; it won't be part of the --strict
suite of compiler options.
Until and unless it is merged, you'll just have to deal with it, and even if it is merged, you might not be able to use that compiler option right away if you depend on any libraries that it breaks. For now, you should just be careful with readonly
properties.
- reactjs - NPM SEMANTIC RELEASE | MAINTENANCE BRANCH - Stack Overflow
- Unable to create a azure synapse workspace? - Stack Overflow
- open source - Langgraph State not being passed properly with Ollama - Stack Overflow
- graphdb - vector embedding on ontotext similarity plugin - Stack Overflow
- python - How to fix autocomplete menu flickering in PyCharm on Linux? - Stack Overflow
- javascript - How to create perfect hash with ASCII symbols as input, where output hash is always the same for each ASCII sequenc
- c# - Getting username of logged in user with NegotiateWindows domain credentials - Stack Overflow
- c++ - Access violation on ending the main function scope but only with Visual Studio 2019 and gcc - Stack Overflow
- javascript - Alphabetize options in a select list, retain event listeners - Stack Overflow
- starknet - How to generate and verify a STARK proof from a Cairo program locally? - Stack Overflow
- python - Steps approximation for time series scatter with mean changing every K number of steps using BIC - Stack Overflow
- javascript - Use HTML5 canvas 2d API with WebGL - Stack Overflow
- pytorch - how to get custom column in the model's forward() function when training with Huggingface Trainer? - Stack Ove
- expo - Thread problem with expo_av in React Native app development - Stack Overflow
- c - How does Boehm's Garbage Collector free memory without creating a separate thread for the GC? - Stack Overflow
- java - jOOQ transaction does not work as expected - Stack Overflow
- GHC unable to find mingwinclude directory on Windows - Stack Overflow