What does the “as const” mean in TypeScript and what is its use case?

5

I am confused about the as const cast. I checked a few documents and videos but did not understand it fully.

My concern is what does the as const mean in the code below and what is the benefit of using it?

const args = [8, 5] as const;
const angle = Math.atan2(...args);
console.log(angle);
Share
Improve this question
6
  • 1
    They're constants. They cannot be changed after they've been declared. It's the same was as 1 will always be one (1) in maths, numbers are constants because they cannot be changed. – Chloe Anderson Apr 7 at 19:56
  • 1
    See: dev.to/adamcoster/the-typescript-as-const-trick-2f4o – Terry Apr 7 at 19:57
  • 1
    Possible duplicate of What's the difference between TypeScript const assertions and declarations? – jcalz Apr 7 at 20:02
  • 1
    Have you read the documentation? – axiac Apr 7 at 22:26
  • 1
    @jcalz I think we can leave this open and let this question become that canonical for “what is as const”. Your answer here is really great 😊 – Linda Paiste Apr 8 at 18:24

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue