Typescript - ( Declaration | Definition ) file (.d.ts file)
Table of Contents
About
declaration files describe the library - .d.ts files
They are ambient.
They provide code completion for Javascript.
By default all visible “@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on.
Articles Related
Management
Search
Repository
- See DefinitelyTyped for the repository containing this files for each library.
Installation
Example with lodash
yarn add @types/lodash --dev
// or
npm install -S @types/lodash
Syntax
Global Variable
/** The number of widgets present */
declare var foo: number;
declare const foo: number; // Read only
declare let foo: number; // Block scoped
Function
- declare function declare functions
declare function greet(greeting: string): void;
Objects with Properties (Namespace)
- declare namespace to declare to describe types or values accessed by dotted notation
declare namespace myLib {
function makeGreeting(s: string): string;
let numberOfGreetings: number;
}
Overloaded function
Function - Overloaded Function (Overloaded)
declare function getWidget(n: number): Widget;
declare function getWidget(s: string): Widget[];
Compiler
with Typescript - tsc (official compiler)
tsc --declaration ./js/index.ts
# or
tsc --declaration -p tsconfig.json
where:
- -d or –declaration generates a '.d.ts' file.