What declare in `export declare type Xyz` means vs` export type Xyz`

It is permissible to write both in the definition file:

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

      

The key word declare

here is inappropriate, right?

+3


source to share


1 answer


Right. The keyword declare

is useful when you need to say that there will be a variable or constant at runtime.

Example. Let's say you want to import a library someExternalLib

, but it's not on npm (you need to manually include it using the script tag). You know that it will be available as a global variable someExternalLib

with functions fun1

and fun2

. The problem is that Typescript doesn't know why you need it by declaring a global someExternalLib

:



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

      

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

+4


source







All Articles