Can a typescript parameter be annotated as const?

There is no way to do it right now, and it may not be, as it is not possible in ES6 either:

How to make function parameter constant in JavaScript?

This workaround doesn't work for TypeScript anyway.


It's not supported yet, but you can upvote this feature request & follow it for any updates in the future: https://github.com/microsoft/TypeScript/issues/18497

In the meantime your best bet is to use a linter rule to make arguments const by default, like ESLint's no-param-reassign rule. In any rare cases where you need an argument to be not const, you can either use eslint-disable-line each place you reassign it, or wrap the whole function in an eslint-disable / eslint-enable pair to make all its arguments non-const.

Tags:

Typescript