Gulp If you’re using Gulp in some fashion, we have a tutorial on using Gulp with TypeScript, and integrating with common build tools like Browserify, Babelify, and Uglify. You can read more there.
Global-modifying Modules A global-modifying module alters existing values in the global scope when they are imported. For example, there might exist a library which adds new members to String.prototype when imported. This pattern is somewhat dangerous due to the possibility of runtime conflicts, but we can still write a declaration file for it.
Global Variables Documentation The global variable foo contains the number of widgets present. Code console.log("Half the number of widgets is " + (foo / 2)); Declaration Use declare var to declare variables. If the variable is read-only, you can use declare const. You can also use declare let if the variable is block-scoped. /** The number of widgets present */ declare var foo: number;
Global Functions Documentation You can call the function greet with a string to show a greeting to the user. Code greet("hello, world"); Declaration Use declare function to declare functions. declare function greet(greeting: string): void;
Global augmentation You can also add declarations to the global scope from inside a module: // observable.ts export class Observable<T> { // ... still no implementation ... } declare global { interface Array<T> { toObservable(): Observable<T>; } } Array.prototype.toObservable = function () { // ... } Global augmentations have the same behavior and limits as module augmentations.
Global Plugin A global plugin is global code that changes the shape of some global. As with global-modifying modules, these raise the possibility of runtime conflict. For example, some libraries add new functions to Array.prototype or String.prototype.
// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~] // Project: [~THE PROJECT NAME~] // Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]> /*~ This is the global-modifying module template file. You should rename it to index.d.ts *~ and place it in a folder with the same name as the module. *~ For example, if you were writing a file for "super-greeter", this *~ file should be 'super-greeter/index.d.ts' */ /*~ Note: If your global-modifying module is callable or co
Global Libraries A global library is one that can be accessed from the global scope (i.e. without using any form of import). Many libraries simply expose one or more global variables for use. For example, if you were using jQuery, the $ variable can be used by simply referring to it: $(() => { console.log('hello!'); } ); You’ll usually see guidance in the documentation of a global library of how to use the library in an HTML script tag: <script src="http://a.great.cdn.for/someLib.js">
// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~] // Project: [~THE PROJECT NAME~] // Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]> /*~ This template shows how to write a global plugin. */ /*~ Write a declaration for the original type and add new members. *~ For example, this adds a 'toBinaryString' method with to overloads to *~ the built-in number type. */ interface Number { toBinaryString(opts?: MyLibrary.BinaryFormatOptions): string; toBinaryString(
Generics Don’t ever have a generic type which doesn’t use its type parameter. See more details in TypeScript FAQ page.
Page 19 of 29