Skip to main content

Declarations

Declarations are a very important part of TypeScript due to which static typing is projected onto dynamic JavaScript.

Declaration#

Since the development of programs in TypeScript uses libraries written in JavaScript, the tsc compiler, whose main task is type checking, feels like it is blindfolded. Despite the fact that with each new version type inference is getting better and better at understanding JavaScript, it is still far from ideal. In addition, parsing the JavaScript code adds a load on the processor, which is sometimes not enough precious time when developing modern applications.

TypeScript solved this problem by connecting to the project the declarations it generated in advance or manually created by developers. Declarations are placed in files with the extension .d.ts and consist only of type declarations that completely repeat the program until the moment of compilation, when it was deprived of all typing features. Their operation is very similar to the operation of files with the extension .h in languages C/C++.

// Файл Animal.ts
export default class Animal {
public name: string = 'animal';
public voice(): void {}
}
// Файл Animal.d.ts
declare module "Animal" {
export default class Animal {
name: string;
voice(): void;
}
}

Payment#

Now you are on a stripped-down version of the site, after subscribing to Patreon, you will get full access to the training course, as well as access to our server's private channels in Discord.

Download our mobile application or get tested in our JavaScript telegram bot, and also subscribe to our news.

Become a Patron!

Sumerian school

Contributors ✨#

Thanks goes to these wonderful people (emoji key):


IIo3iTiv

📖

Dmitriy Vasilev

💵

Become a Patron!