Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does declare in `export declare type Xyz` mean vs `export type Xyz`

Tags:

In a definition file it is valid to write both:

export declare type Abc = string;
export type Bcd = string;

The declare keyword here serves no purpose, correct?

like image 841
AJP Avatar asked Apr 22 '17 16:04

AJP


1 Answers

Correct. declare keyword is useful when you need to say that there will be a variable or constant at execution time.

Example: Let's say you want to import library someExternalLib, but it is not on npm (you have to manually include it via script tag). You know that it will be accessible as global variable someExternalLib with functions fun1 and fun2. The problem is that Typescript doesn't know - that's why you have to help it by declaring the global someExternalLib:

declare const someExternalLib: { fun1: () => number, fun2: () => number }

This is usually necessary in definition files to declare variables, constants, classes, functions. It is redundant for types and interfaces.

like image 76
Erik Cupal Avatar answered Oct 30 '22 14:10

Erik Cupal