For example, when you want to work with JavaScript code which looks like:

    1. import greeter from "super-greeter";
    2. greeter(2);
    3. greeter("Hello world");

    To handle both importing via UMD and modules:

    1. // Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
    2. // Project: [~THE PROJECT NAME~]
    3. // Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>
    4. /*~ This is the module template file for function modules.
    5. *~ You should rename it to index.d.ts and place it in a folder with the same name as the module.
    6. *~ For example, if you were writing a file for "super-greeter", this
    7. *~ file should be 'super-greeter/index.d.ts'
    8. */
    9. // Note that ES6 modules cannot directly export class objects.
    10. // This file should be imported using the CommonJS-style:
    11. // import x = require('[~THE MODULE~]');
    12. //
    13. // Alternatively, if --allowSyntheticDefaultImports or
    14. // --esModuleInterop is turned on, this file can also be
    15. // imported as a default import:
    16. // import x from '[~THE MODULE~]';
    17. //
    18. // Refer to the TypeScript documentation at
    19. // https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
    20. // to understand common workarounds for this limitation of ES6 modules.
    21. /*~ If this module is a UMD module that exposes a global variable 'myFuncLib' when
    22. *~ loaded outside a module loader environment, declare that global here.
    23. *~ Otherwise, delete this declaration.
    24. */
    25. export as namespace myFuncLib;
    26. /*~ This declaration specifies that the function
    27. *~ is the exported object from the file
    28. */
    29. export = Greeter;
    30. /*~ This example shows how to have multiple overloads for your function */
    31. declare function Greeter(name: string): Greeter.NamedReturnType;
    32. declare function Greeter(length: number): Greeter.LengthReturnType;
    33. /*~ If you want to expose types from your module as well, you can
    34. *~ place them in this block. Often you will want to describe the
    35. *~ shape of the return type of the function; that type should
    36. *~ be declared in here, as this example shows.
    37. *~
    38. *~ Note that if you decide to include this namespace, the module can be
    39. *~ incorrectly imported as a namespace object, unless
    40. *~ --esModuleInterop is turned on:
    41. *~ import * as x from '[~THE MODULE~]'; // WRONG! DO NOT DO THIS!
    42. */
    43. declare namespace Greeter {
    44. export interface LengthReturnType {
    45. width: number;
    46. height: number;
    47. }
    48. export interface NamedReturnType {
    49. firstName: string;
    50. lastName: string;
    51. }
    52. /*~ If the module also has properties, declare them here. For example,
    53. *~ this declaration says that this code is legal:
    54. *~ import f = require('super-greeter');
    55. *~ console.log(f.defaultName);
    56. */
    57. export const defaultName: string;
    58. export let defaultLength: number;
    59. }