• [Typescript] Declare Module


    https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules

    Example for declare node.js "url" & "path" module:

    node.d.ts

    declare module "url" {
      export interface Url {
        protocol?: string;
        hostname?: string;
        pathname?: string;
      }
      export function parse(
        urlStr: string,
        parseQueryString?,
        slashesDenoteHost?
      ): Url;
    }
    declare module "path" {
      export function normalize(p: string): string;
      export function join(...paths: any[]): string;
      export var sep: string;
    }

    Then we can add below code to the file

    /// <reference path="node.d.ts"/>

    Then we can import those modules and using it:

    /// <reference path="node.d.ts"/>
    import * as URL from "url";
    // import url = require("url")
    let myUrl = URL.parse("https://www.typescriptlang.org");

    Shorthand ambient modules

    If you don’t want to take the time to write out declarations before using a new module, you can use a shorthand declaration to get started quickly.

    declarations.d.ts
    declare module "hot-new-module";

    All imports from a shorthand module will have the any type.

    import x, { y } from "hot-new-module";
    x(y);
  • 相关阅读:
    JVM和HotSpot
    java中的四种引用类型
    垃圾回收与算法
    Full GC
    JVM内存结构
    事务不同的隔离级别实现原理
    事务的隔离级别
    jQuery后续和 前端框架Bootstrap
    jQuery
    BOM和DOM操作
  • 原文地址:https://www.cnblogs.com/Answer1215/p/16824872.html
Copyright © 2020-2023  润新知