Why do you need the `declare` keyword in TypeScript? Explain with examples
Hi everyone! I started learning TypeScript and came across the `declare` keyword. The official documentation says it is used for "declaring variables, functions, and classes that exist somewhere else." But I don't quite understand why it's needed and how it works in practice.\n\nFor example, I'm writing a simple script that uses the global variable `$` (jQuery). I know it's loaded via CDN in HTML, but TypeScript complains that `$` is not defined. I tried writing:\n\n```typescript\ndeclare var $: any;\n```\n\nAnd the error disappeared. But why can't I just write `var $: any;`? What's the difference? If I write `let $: any;`, that also works, but then TypeScript will think I'm creating a new variable, not describing an existing one.\n\nI also saw that `declar
No answers yet.
Log in to leave an answer
Blogs
What is a rune in Go? Comparison of approaches and real examples
Handling JSON Post Request in Go: 5 Fatal Mistakes and How to Avoid Them
How to Remove the Last Delimiter from a String: PHP Under the Hood
Best Python Books for Beginners: Top Picks for 2025