Argument expressions should be as const
when possible
#31883
Labels
Duplicate
An existing issue was already created
as const
when possible
#31883
Search Terms
"as const" arguments parameters "return type"
Suggestion
With the introduction of
as const
all expressions to functions should be treatedas const
Use Cases/Examples
Consider this case
https://www.typescriptlang.org/play/#src=function%20takesTuple%3CT%20extends%20readonly%20any%5B%5D%3E(t%3A%20T)%20%7B%0A%20%20return%20t%0A%7D%0A%0Aconst%20tuple1%3A%20%5Bnumber%2C%20string%5D%20%3D%20%5B1%2C%20'foo'%5D%3B%0Aconst%20tuple2%20%3D%20takesTuple(tuple1)%3B%0A%0Aconst%20tuple3%20%3D%20%5B1%2C%20'bar'%5D%20as%20const%3B%0Aconst%20tuple4%20%3D%20takesTuple(tuple3)%3B%0A%0A%2F%2F%20This%20should%20return%20%60%5B2%2C%20'baz'%5D%60%20or%20at%20least%20%60%5Bnumber%2C%20string%5D%60%0A%2F%2F%20instead%20returns%20%60Array%3Cnumber%20%7C%20string%3E%60%0Aconst%20bad%20%3D%20takesTuple(%5B2%2C%20'baz'%5D)%3B%0Abad%3B%0A%0A%2F%2F%20You%20shouldn't%20need%20to%20have%20%22as%20const%22%20for%20expressions%0Aconst%20good%20%3D%20takesTuple(%5B2%2C%20'baz'%5D%20as%20const)%3B%0Agood
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: