Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a type globally in a project (typescript)

Tags:

typescript

People also ask

How do I declare a variable globally in TypeScript?

To declare a global variable in TypeScript, create a . d. ts file and use declare global{} to extend the global object with typings for the necessary properties or methods.

Where do you declare global type variables?

In Python and MATLAB a global variable can be declared anywhere with the global keyword. Ruby's global variables are distinguished by a ' $ ' sigil.

What is type declaration in TypeScript?

TypeScript includes declaration files for all of the standardized built-in APIs available in JavaScript runtimes. This includes things like methods and properties of built-in types like string or function , top-level names like Math and Object , and their associated types.

What is D ts in TypeScript?

The "d. ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using something like jQuery or underscore, an existing javascript library. You want to consume those from your typescript code.


You can create a definition file, ending with the .d.ts extension, and place it in your project where you'd like:

user.d.ts

interface IUser {
    name: string,
    mail: string
}

This is also a good way to fill missing global types. I have a lib/global folder per project to place these files.

This only works with type definitions, not actual code, as (1) that actual code would have to be imported some way, (2) .d.ts is ambient. Also, for types defined this way to be globally visible, the corresponding declaration file should not contain top-level exports (otherwise the declaration file will be regarded as a module, requiring an import to access its types).


You can also declare modules:

declare module "my-module" {
    export declare class Something {
        public something: number;
    }
}

And then TypeScript will allow the following:

import { Something } from "my-module";

You can define types in multiple files and use them in other files. There are two possble ways:

  1. The files are using the same namespace. In this case the type can be reused directly.
  2. The files are using different namespaces. In this case the reusable type has to be marked with export. It can then be used in other namespaces. With the import statement you can avoid having to use the namespace prefix.

In any case, you have to add a reference to the file whose types you want to reuse.

Same namespace

File 1:

namespace Example {
    export interface IUser {
       name: string,
       mail: string
    }
}

File 2:

/// <reference path="./File1.ts" />

namespace Example {
    class User implements IUser {
       name: string,
       mail: string
    }
}

Different namespaces

File 1:

namespace Example {
    export interface IUser {
       name: string,
       mail: string
    }
}

File 2 (no import):

/// <reference path="./File1.ts" />

class User implements Example.IUser {
   name: string,
   mail: string
}

File 2 (with import):

/// <reference path="./File1.ts" />

import IUser = Example.IUser;

class User implements IUser {
   name: string,
   mail: string
}

Create global.d.ts in your project root.

Here is an example of how to declare global types constants and interfaces that will be understood globally

//example import for externally loaded library
//this will not form part of the bundle but will allow typescript to understand external libraries throughout the project
import _ from 'lodash'; 

declare global { 
    //Example global constant for libraries served via webpack externals. example webpack config:: externals: { _: 'lodash' }
    //This assumes lodash was already load in DOM for example in <head> via CDN link before main.js is loaded.
    const _: typeof _; 

    //example of custom types
    type JSONPrimitive = string | number | boolean | null;
    type JSONValue = JSONPrimitive | JSONObject | JSONArray;
    type JSONObject = { [member: string]: JSONValue };

    //example of custom interface
    interface JSONArray extends Array<JSONValue> {}
}