-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default to ReadonlyArray #32467
Comments
Are you saying you want, //ts
export const arr = [1,2,3]; To emit, //.d.ts
export const arr : readonly number[]; ? |
@AnyhowStep I'm not familiar with the syntax you used in your //ts
export const arr = [1,2,3]; to emit //.d.ts
export const arr: ReadonlyArray<number>; |
class Test {
readonly value = [0, 1, 2, 3] as const; // <-- type `readonly [0, 1, 2, 3]` here
} |
I see. Thanks @demensky so there's this (was not familiar with this feat): EDIT: Maybe this issue can be turned into: Option to default to
|
Do note there's a subtle difference - It also infers the strictest possible type for each element (literal type) rather than the more general |
@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 |
I'd like this feature, but TypeScript should remain intuitive to plain JavaScript users IMO, so it should live behind a I'd also love to have this for interface properties, defaulting to interface T {
n: number // immutable
mutable s: string // mutable
}
const o: T = {
n: 42,
s: 'hello world',
}
o.n = 43 // error
o.s = '👋🌎' // ok 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. |
Good idea! However, I would like it also for interface properties. At the moment, my code has too much |
|
Having a flag which will turn on "assume everything is read only" would be great! I'm using UPDATE: I've opened proposal for this here #42357 |
@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 |
This is already the case since |
I think we can close this one in favor of the proposal above |
Search Terms
ReadonlyArray, default
Suggestion
By default type for array is inferred as
Array
. It would be better to default to a more strictReadonlyArray
and if programmer needs mutability — he would explicitly state so. Could be atsconfig
option to not introduce a breaking change.Use Cases
readonly value = [0, 1, 2, 3]; // <-- type number[] here
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: