首页 > 其他分享 >[Typescript] Declare Module

[Typescript] Declare Module

时间:2022-10-25 15:15:26浏览次数:73  
标签:Typescript string url modules module export Module import Declare

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);

 

标签:Typescript,string,url,modules,module,export,Module,import,Declare
From: https://www.cnblogs.com/Answer1215/p/16824872.html

相关文章

  • Vue笔记19 Vuex - Module
                                   ......
  • [Typescript] 67. Medium - Chunk
    Doyouknow lodash? Chunk isaveryusefulfunctioninit,nowlet'simplementit. Chunk<T,N> acceptstworequiredtypeparameters,the T mustbea tu......
  • [Typescript] TypeScript module Augmentation
    Youmighthavesomechangeslocallyfor3rdpartylibrary.Forthelocalimplementation,youneedtomodifythetypesinordertoresolveIDEissue. Whenme......
  • Module
    模块CommonJs(用于服务器),AMD(用于浏览器)//CommonJS模块,生成对象let{stat,exists,readfile}=require('fs');//ES6模块import{stat,exists,readFile......
  • Typescript类型体操 - Trunc
    题目中文实现类型版本的Math.trunc,其接受一个字符串或数字作为泛型参数,并返回移除了全部小数位部分后的整数示例:typeA=Trunc<12.34>;//12EnglishImpleme......
  • Typescript类型体操 - TrimRight
    题目中文实现TrimRight<T>,它接收确定的字符串类型并返回一个新的字符串,其中新返回的字符串删除了原字符串结尾的空白字符串。例如typeTrimed=TrimRight<'Hello......
  • TypeScript 高级类型
    一、高级类型class类型兼容性交叉类型泛型和keyof索引签名类型和索引查询类型映射类型二、class构造函数给变量赋值extends继承implement实现接口//感叹......
  • ModuleNotFoundError: No module named 'cv2' in Python
    ModuleNotFoundError:Nomodulenamed'cv2'inPythonModuleNotFoundError:Nomodulenamed'cv2'inPythonsudopip3installopencv-pythonpip3installopencv-p......
  • IIS 7.5 Application Warm-Up Module
    有些web应用在可以处理用户访问之前,需要装载很多的数据,或做一些花费很大的初始化处理。今天使用ASP.NET的开发人员经常使用应用的Global.asax 文件中......
  • BrowserModule has already been loaded. If you need access to common directives s
    错误信息:BrowserModulehasalreadybeenloaded.IfyouneedaccesstocommondirectivessuchasNgIfandNgForfromalazyloadedmodule,importCommonModulein......