You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default type for array is inferred as Array. It would be better to default to a more strict ReadonlyArray and if programmer needs mutability — he would explicitly state so. Could be a tsconfig option to not introduce a breaking change.
Use Cases
readonly value = [0, 1, 2, 3]; // <-- type number[] here
I see. Thanks Dmytro Demensky (@demensky) so there's this (was not familiar with this feat): https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#const-assertions
That's a bit shorter than explicitly stating ReadonlyArray but in terms of arrays I like never use mutability and very rarely do for objects, it would be great if I could force that thing on a tsconfig level like I can force things like strict null checks, no implicit any, strictPropertyInitialization, stuff like that.
EDIT: Maybe this issue can be turned into:
Option to default to as const for objects and arrays with added as mutable assertion for mutability
Do note there's a subtle difference - as const when applied to an array literal infers a tuple, not an array. At runtime tuples and arrays are the same of course, but there are differences in how the type system treats them.
It also infers the strictest possible type for each element (literal type) rather than the more general number or string, so that's something to keep in mind too.
Bruce Pascoe (@fatcerberus) true, that's why I'm focusing on array and object since it wouldn't matter for them for the most part, and for numbers and strings we can just declare them as const and not let.
I'd like this feature, but TypeScript should remain intuitive to plain JavaScript users IMO, so it should live behind a -strict-array-immutability flag.
I'd also love to have this for interface properties, defaulting to readonly with a -strict-interface-immutability flag and having to mark mutable ones with a mutable keyword, as such:
The interface is a separate suggestion and should probably live in its own GitHub issue, but it makes sense to mention it in this discussion since the semantics/syntax should be similar.
Related: #22315, this comment in particular also requests a flag under the strict umbrella for readonly-by-default.
Having a flag which will turn on "assume everything is read only" would be great!
I'm using readonly in all the places and it's too noisy. I would prefer mutable flag for places where I do need mutation which I don't in 99% of the time.
Irakli Safareli (@safareli)'s suggestion should be a proposal by its own, probably. The problem is, how will we consume 3rd party libraries which cannot have their own flags scoped to their code? Library ecosystem would be divided into 2 which is not desirable.
We probably need per module/file flags like Haskell's lang. extensions. It would behave similar to the @jsx pragma. Only a subset of flags can be used with in-file pragmas. So gotta extend @jsx pragma feature to a more generic feature.
Library ecosystem would be divided into 2 which is not desirable.
This is already the case since strictNullChecks was introduced in TypeScript 2.0 (in 2016). Even nowadays many popular libraries are lacking type-safety by not utilizing the strict compiler flags.
Activity
AnyhowStep commentedon Jul 18, 2019
Are you saying you want,
To emit,
?
waterplea commentedon Jul 19, 2019
AnyhowStep I'm not familiar with the syntax you used in your
.d.ts. I want a tcsconfig option forto emit
demensky commentedon Jul 19, 2019
waterplea commentedon Jul 19, 2019
I see. Thanks Dmytro Demensky (@demensky) so there's this (was not familiar with this feat):
https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#const-assertions
That's a bit shorter than explicitly stating
ReadonlyArraybut in terms of arrays I like never use mutability and very rarely do for objects, it would be great if I could force that thing on a tsconfig level like I can force things like strict null checks, no implicit any, strictPropertyInitialization, stuff like that.EDIT: Maybe this issue can be turned into:
Option to default to
as constfor objects and arrays with addedas mutableassertion for mutabilityfatcerberus commentedon Jul 19, 2019
Do note there's a subtle difference -
as constwhen applied to an array literal infers a tuple, not an array. At runtime tuples and arrays are the same of course, but there are differences in how the type system treats them.It also infers the strictest possible type for each element (literal type) rather than the more general
numberorstring, so that's something to keep in mind too.waterplea commentedon Jul 19, 2019
Bruce Pascoe (@fatcerberus) true, that's why I'm focusing on array and object since it wouldn't matter for them for the most part, and for numbers and strings we can just declare them as
constand notlet.lautarodragan commentedon Aug 8, 2019
I'd like this feature, but TypeScript should remain intuitive to plain JavaScript users IMO, so it should live behind a
-strict-array-immutabilityflag.I'd also love to have this for interface properties, defaulting to
readonlywith a-strict-interface-immutabilityflag and having to mark mutable ones with amutablekeyword, as such:The interface is a separate suggestion and should probably live in its own GitHub issue, but it makes sense to mention it in this discussion since the semantics/syntax should be similar.
Related: #22315, this comment in particular also requests a flag under the strict umbrella for readonly-by-default.
thehappycoder commentedon Jan 19, 2020
Good idea! However, I would like it also for interface properties. At the moment, my code has too much
readonlykeywords.Shinigami92 commentedon Jan 27, 2020
readonly value = [1, 2, 3, 4, 5]means only that you cant reassignvalueBut you can add more elements into
value, sovalue.push(6)works finesafareli commentedon May 4, 2020
Having a flag which will turn on "assume everything is read only" would be great!
I'm using
readonlyin all the places and it's too noisy. I would prefermutableflag for places where I do need mutation which I don't in 99% of the time.UPDATE: I've opened proposal for this here #42357
anilanar commentedon Jan 15, 2021
Irakli Safareli (@safareli)'s suggestion should be a proposal by its own, probably. The problem is, how will we consume 3rd party libraries which cannot have their own flags scoped to their code? Library ecosystem would be divided into 2 which is not desirable.
We probably need per module/file flags like Haskell's lang. extensions. It would behave similar to the
@jsxpragma. Only a subset of flags can be used with in-file pragmas. So gotta extend@jsxpragma feature to a more generic feature.MartinJohns commentedon Jan 15, 2021
This is already the case since
strictNullCheckswas introduced in TypeScript 2.0 (in 2016). Even nowadays many popular libraries are lacking type-safety by not utilizing thestrictcompiler flags.safareli commentedon Jan 15, 2021
Anıl Anar (@anilanar) I've opened the proposal #42357
waterplea commentedon Nov 8, 2021
I think we can close this one in favor of the proposal above