没有定义实现的声明

Preview
1 环境模块 Ambient Modules
@types/node
@types/node/url.d.ts
ts
export interface Url {
protocol?: string;
hostname?: string;
pathname?: string;
}
export function parse(
urlStr: string,
parseQueryString?,
slashesDenoteHost?
): Url;
@types/node/path.d.ts
ts
export function normalize(p: string): string;
export function join(...paths: any[]): string;
export var sep: string;
另一种写法是使用 declare module
将这些类型声明写在一个 .d.ts
文件中,它的结构类似于命名空间。
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;
}
2 速记环境模块 Shorthand ambient modules
如果不想在使用新模块之前花时间写声明,可以使用速记声明来快速上手。
declarations.d.ts
ts
declare module "hot-new-module";
来自速记模块的所有导入都将具有any类型。
ts
import x, { y } from "hot-new-module";
x(y);