Avatar ·

What does "as const" mean in TypeScript and why is it needed?

Hello everyone! I started learning TypeScript and came across the `as const` construct. I understand that it's somehow related to type assertion, but I can't fully figure it out.\n\nHere's an example that confuses me:\n\n```typescript\nconst colors = ['red', 'green', 'blue'] as const;\n// typeof colors -> readonly ["red", "green", "blue"]\n\nconst myColor = 'red' as const;\n// typeof myColor -> "red" (literal type, not string)\n\nconst config = {\n api: 'https://api.example.com',\n port: 3000\n} as const;\n// typeof config -> { readonly api: "https://api.example.com"; readonly port: 3000 }\n```\n\nI see that the types change, but I don't understand:\n\n1. Why does just `const colors = ['red', 'green', 'blue']` give the type `string[]`, but with `as const` it gives a tuple with specific literals?\n2. Why would you even need to specify