0 votes
in TypeScript - JavaScript's Superset by
What is TypeScript Declare Keyword?

1 Answer

0 votes
by

We know that all JavaScript libraries/frameworks don't have TypeScript declaration files, but we want to use them in our TypeScript file without any compilation errors. To do this, we use the declare keyword. The declare keyword is used for ambient declarations and methods where we want to define a variable that may exist elsewhere.

For example, suppose we have a library called myLibrary that doesn't have a TypeScript declaration file and have a namespace called myLibrary in the global namespace. If we want to use that library in our TypeScript code, we can use the following code:

declare var myLibrary;  

TypeScript runtime will assign the myLibrary variable as any type. Here is a problem that we won't get Intellisense in design time but we will be able to use the library in our code.

Related questions

0 votes
asked Mar 23, 2022 in TypeScript - JavaScript's Superset by sharadyadav1986
0 votes
asked Mar 23, 2022 in TypeScript - JavaScript's Superset by sharadyadav1986
...