Definition file for module library

The definition file for a library is similar but also different. If you need to provide a definition file, it is recommended to name an index.d.ts with the following rules. First, there is an optional export declaration that is needed if the library supports UMD. This happens when the library exports a variable. The variable exposed in the following code example is myScope, where the whole module resides:

export as namespace myScope;

The next steps are to add every function directly to the definition file. There is no need to englobe the functions into a namespace. This is the same for an object:

export function myFunction(): void;
export interface MyObject{
x: number;
}
export let data: string;

The usage of the function, interface, and variable would be like this in the actual code:

import {myFunction, MyObject, data} from “myScope”;
myFunction();
x:MyObject = {x:1};
console.log(data);

The remaining feature would be to have an object inside your module:

export namespace myProperty{
export function myFunction2(): void;
}

Here is the actual usage of the namespace, which, like the functions, object, and data, is available in two formats. The first is with the explicit callout of the element to be retrieved from the module or the second with the star, which gets the whole content definition into the alias:

Import {myProperty} from “myScope”;
myProperty.myFunction2();
//or
Import * from my from “myScope”
my.myProperty.myFunction2();

However, in some cases, this won't work. It depends how the JavaScript module is written. The following code works for modern module creation. It uses the declare statement with a module. The module name must be the library name between quotes. Inside the module, you can define your export for CommonJs/Amd with export =, followed by what you want to export by default. In the following code, the class MessageFormat is the default export. It is possible to not have a CommonJs/Amd export and to export every type. You can export a namespace that contains many types as well:

declare module "modulenamehere" {
type Msg = (params: {}) => string;
type SrcMessage = string | SrcObject;
interface SrcObject {
m1: SrcMessage;
}

class MessageFormat {
constructor(message: string);
constructor();
compile: (messages: SrcMessage, locale?: string) => Msg;
}

export = MessageFormat ; // CommonJs/AMD export syntax
}

// Usage:

import MessageFormat from "modulenamehere";
const mf = new MessageFormat("en");
const thing = mf.compile("blarb");

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.63.208