What does "as const" mean in TypeScript and when to use it?
Hello, colleagues! I recently started learning TypeScript and came across the `as const` construct. Honestly, I don't fully understand what it does and why it's needed.\n\nHere's an example I found in the documentation:\n\n```typescript\nconst colors = ['red', 'green', 'blue'] as const;\n```\n\nI understand that `as` is used for type assertion, for example, `someValue as string`. But what does adding `const` after `as` mean? How is this different from simply declaring an array or object?\n\nI tried writing this code:\n\n```typescript\nconst fruits = ['apple', 'banana'];\nconst fruitsConst = ['apple', 'banana'] as const;\n\n// What is the difference between fruits and fruitsConst?\n// Why can't I modify fruitsConst?\n```\n\nI also noticed that when I use `as const` with objects, Typ
No answers yet.
Log in to leave an answer
Blogs