Basics

Challenges

Classes

Cookies

Debugging

Events

External Files

Flow Control

Forms

Functions

Html Elements

Installation

Interfaces

Keywords

Modules

Namespaces

Operators

Reference Files

String

Types

TypeScript - declare

declare is used for ambient declarations where you want to define a variable that may not have originated from a TypeScript file. For example, lets imagine that we have a library called myLibrary that doesn't have a TypeScript declaration file and have a namespace called myLibrary in the global namespace.

declare is a special keyword in Typescript which creates an ambient declaration. It basically says "document" (or whatever) is going to be something that is referred to and I don’t necessarily have it in my file.

When Doing this, you are making an ambient declaration. Ambient declarations are a way of telling the TypeScript compiler that the actual source code exists elsewhere. When you are consuming a bunch of third party js libraries like jquery/angularjs/nodejs - you can’t rewrite it in TypeScript. Ensuring type-safety and intellisense while using these libraries will be challenging for a TypeScript programmer. Ambient declarations help to seamlessly integrate other js libraries into TypeScript.

Ambient declarations are by convention kept in a type declaration file with following extension (d.ts)

Sample.d.ts