Skip to content
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

Argument expressions should be as const when possible #31883

Closed
4 of 5 tasks
kolodny opened this issue Jun 13, 2019 · 2 comments
Closed
4 of 5 tasks

Argument expressions should be as const when possible #31883

kolodny opened this issue Jun 13, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@kolodny
Copy link

kolodny commented Jun 13, 2019

Search Terms

"as const" arguments parameters "return type"

Suggestion

With the introduction of as const all expressions to functions should be treated as const

Use Cases/Examples

Consider this case

function takesTuple<T extends readonly any[]>(t: T) {
  return t
}

const tuple1: [number, string] = [1, 'foo'];
const tuple2 = takesTuple(tuple1);

const tuple3 = [1, 'bar'] as const;
const tuple4 = takesTuple(tuple3);

// This should return `[2, 'baz']` or at least `[number, string]`
// instead returns `Array<number | string>`
const bad = takesTuple([2, 'baz']);
bad;

// You shouldn't need to have "as const" for expressions
const good = takesTuple([2, 'baz'] as const);
good

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:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@dragomirtitian
Copy link
Contributor

Duplicate of #30680 I believe

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 13, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants