-
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
New Feature Requesting for const
Parameter
#18497
Comments
const
parameterconst
Parameter
This is essentially covered by #17502 and related to #9741 specifically this comment |
@kitsonk Oh, thanks. I see, and let's say this is another useful example, which affects the way we code. |
tslint has a rule for this. |
@andy-ms That may be a little overdo, since there are some other parameters need reassignment. |
I think better to use function foo(readonly constantProperty: any) {
constantProperty = 1 // Error. Couldn't assign a readonly parameter.
} |
@ngolin tslint has options for overriding errors. I can't imagine this is common enough for that to be burdensome? |
The |
@yxliang01 You can add |
Thanks @andy-ms . However, it's still not ideal... I think it will make the code messier. Also, there's no control over individual parameters... |
@brn |
@ajafff why should it be a tslint rule instead of a declaration like what other languages do? |
@yxliang01 |
That could be argued about the whole type system. 😉 It is very arguable that re-assignment of parameters is something that an end user would want to control, as it can lead to logic errors in the code, and is the main goal of TypeScript. On the other hand, I would much rather see #17502 more fully addressed, because it feels like is addressing the problem space versus a point feature. |
I meant it is noise in the interface declaration. since mutation of the parameter inside the method, which is a terrible practice, does not affect the caller the information is not useful. so if I'm consuming something from source and I see a bunch of annotations on parameters stating that their immutable but I'm not changing the code of those functions the information is not helpful to me. I suppose it could be argued that its presence indicates potentially higher quality or more thoughtfully written functions. |
everytime I look at some Java code, which fortunately isn't very often, they strike me as implementation details exposed in the same location as a functions interface. this doesn't apply to type information in general. It does apply to parameter destructuring, which is also noise for the source level consumer, but I digress... Anyway, Scala got this one right. |
@yxliang01 I'd like to see this as part of the language. I just don't think Until this is implemented in TypeScript, you can use the lint rule I just wrote: https://github.com/ajafff/tslint-consistent-codestyle/blob/master/docs/const-parameters.md. In contrast to tslint's |
Most functions do not assign their parameters. A systematic use of I think this kind of assignment should be impossible. However, this could break a lot of code. A compiler option seems the best option... or the use of a linter? |
I certainly need this one! 👍 |
I put |
@lppedd Nice to know that Kotlin do that right. |
Are these issues reflected in typescript? Or should we use only |
Searching around google and got here. I feel like this should be part of ts instead of tslint. Is it still under discussing? |
I really want this |
I think my use case is slightly different from the ones described above I have a function with a type signature similar to this (much simplified) const fn = <T> (x: T) => ({ key: x as T }); When I call I would like a way to enforce that Something like const fn = <T> (const x: T) => ({ key: x as T });
// or
const fn = <T> (readonly x: T) => ({ key: x as T }); would be great |
type UnitsOnly<T> = string extends T ? never : T;
const fn = <T extends string>(x: UnitsOnly<T>) => ({ key: x });
fn("ok");
fn("not ok" as string); |
I think this is a good idea. |
I want this. Why is this closed? |
this is an excellent idea, I really want this now. |
@ngolin could you please reopen this? |
I've read this, and skimmed #17502, as well as #9741. I don't think my argument is covered anywhere else, and I think it's a reasonably good one. I use this pattern a lot: function arrFind(obj: string[], term?: string) {
if (!term) { return DEFAULT_RESULT; }
return obj.find(x => x.includes(term));
} This will fail because the function arrFind(obj: string[], term?: string) {
const searchTerm = term;
if (!searchTerm) { return DEFAULT_RESULT; }
return obj.find(x => x.includes(searchTerm));
} This is added verbosity for the sake of making the compiler happy, and I probably did it 200 times in my current project. If I could simply declare the parameters as |
Thanks for reopening it 👍
@RyanCavanaugh i understand, from my point of view TS is more reliable for this implementation than using an ESLint rule, maybe am I wrong 🤔 |
I don't understand how a lint rule would make the OP's example code work without introducing an extra variable, as in the second code block ( |
The thing we've discussed (offline?) is just having a setting for "all parameters are always const", since in practice it isn't much of a burden to always write code that way. Introducing an actual inline |
why not keep function awesome(x: readonly number) {} and i'd love to have a flag, like function awesomer(x: mutable number) {} |
I love the idea of making things immutable by default. From RustLang we already know that it's a good idea to make good design choices the simplest way to code. However, this is TypeScript, which want to appeal to JavaScript devs who want to take their coding to the next level. Switching the default behavior around sounds like inviting trouble and many lost developers, plus the tsc does not output good enough error messages and suggestions to guide people. That's why I would love to see some way to mark parameters as const or readonly, something which is already known from other places in TypeScript. |
I have a use case for which I would really like this sort of functionality built into the type-checker. TsLint might be good for linting one's own code, but the checks get lost once build/bundled/distributed. For my use case, I am developing a library that does some state management on behalf of a user. The user interacts with the state in a restricted way so that the library knows to check the state for changes. For example, imagine: class StateManager<S = any> {
constructor (private readonly state: S) {}
public updateState(updater: (state: S) => void) {
updater(this.state)
// do stuff on state change
}
}
// BAD
stateManager.updateState((state) => {
state = { different: "state" }
})
// OK
stateManager.updateState((state) => {
state.prop = "update"
state.nested.prop = 2
}) The bad use case could be avoided with a const/readonly/final type annotation: class StateManager<S = any> {
constructor private state: S
public updateState(updater: (readonly state: S) => void) {
updater(this.state)
// do stuff on state change
}
} |
@connormckelvey TSLint has been deprecated as of 2019 |
Connor might have meant |
👍 |
I have a small new feature to request. Let say we have this piece of code:
Fortunately, there is a way to fix this:
But can we twist the code a little bit, which is what I asking for? Something like this:
So you can see there really are some cases a
const
parameter might come to handy. Thank you.P.S. Just for demonstrating the point, here's a full demo:
The text was updated successfully, but these errors were encountered: