I\'ve read docs (ie, this) and a few blog posts about declare keyword in TypeScript and I still don\'t understand it. What I would like is a clear example of it
Your example is correct. For example you are using a node module which is written in plain javascript (no typings available), so the tscompiler will notice that (since it searches for the typings which are usually in the node module or an extra @typings/package).
You can however provide those typings yourself by telling the tscompiler in the tsconfig.json to look at file xyz.d.ts for the typings e.g.
tsconfig.json
{
"files": [
"typings/index.d.ts"
]
}
The index.d.ts is where all your custom typings are gathered which can look like this
index.d.ts
///
and the custom-typings.d.ts has the actual typings in it. This is where the declare keyword comes into play
custom-typings.d.ts
declare module "the-untyped-node-module-name" {
export default class TheNodeModuleClass {
static showNotification(string: any): void;
}
}
Now the Typescript Compiler knows there is a TheNodeModuleClass in the-untyped-node-module-name which has the static function showNotification.
For more info See Typscript Modules.
This is one use case for the keyword declare. There are of course more of it like declare var, declare function, declare class and so on.