diff --git a/types/node/child_process.d.ts b/types/node/child_process.d.ts index 1cb3f87378..c45f3b8507 100644 --- a/types/node/child_process.d.ts +++ b/types/node/child_process.d.ts @@ -133,7 +133,7 @@ declare module "child_process" { uid?: number; gid?: number; cwd?: string; - env?: NodeJS.ProcessEnv; + env?: NodeJS.Dict; } interface CommonOptions extends ProcessEnvOptions { diff --git a/types/node/cluster.d.ts b/types/node/cluster.d.ts index 2992af8cac..0ef6c2a052 100644 --- a/types/node/cluster.d.ts +++ b/types/node/cluster.d.ts @@ -100,9 +100,7 @@ declare module "cluster" { settings: ClusterSettings; setupMaster(settings?: ClusterSettings): void; worker?: Worker; - workers?: { - [index: string]: Worker | undefined - }; + workers?: NodeJS.Dict; readonly SCHED_NONE: number; readonly SCHED_RR: number; @@ -184,9 +182,7 @@ declare module "cluster" { const settings: ClusterSettings; function setupMaster(settings?: ClusterSettings): void; const worker: Worker; - const workers: { - [index: string]: Worker | undefined - }; + const workers: NodeJS.Dict; /** * events.EventEmitter diff --git a/types/node/globals.d.ts b/types/node/globals.d.ts index 296cd21586..def669a17c 100644 --- a/types/node/globals.d.ts +++ b/types/node/globals.d.ts @@ -669,9 +669,8 @@ declare namespace NodeJS { isTTY?: true; } - interface ProcessEnv { - [key: string]: string | undefined; - } + // Alias for compability + type ProcessEnv = Dict; interface HRTime { (time?: [number, number]): [number, number]; @@ -781,7 +780,7 @@ declare namespace NodeJS { cwd(): string; debugPort: number; emitWarning(warning: string | Error, name?: string, ctor?: Function): void; - env: ProcessEnv; + env: Dict; exit(code?: number): never; exitCode?: number; getgid(): number; @@ -1066,15 +1065,11 @@ declare namespace NodeJS { type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array; type ArrayBufferView = TypedArray | DataView; - interface NodeRequireCache { - [path: string]: NodeModule; - } - interface Require { /* tslint:disable-next-line:callable-types */ (id: string): any; resolve: RequireResolve; - cache: NodeRequireCache; + cache: Dict; /** * @deprecated */ @@ -1087,11 +1082,10 @@ declare namespace NodeJS { paths(request: string): string[] | null; } - interface RequireExtensions { + interface RequireExtensions extends Dict<(m: Module, filename: string) => any> { '.js': (m: Module, filename: string) => any; '.json': (m: Module, filename: string) => any; '.node': (m: Module, filename: string) => any; - [ext: string]: (m: Module, filename: string) => any; } interface Module { exports: any; @@ -1103,4 +1097,12 @@ declare namespace NodeJS { children: Module[]; paths: string[]; } + + interface Dict { + [key: string]: T | undefined; + } + + interface ReadOnlyDict { + readonly [key: string]: T | undefined; + } } diff --git a/types/node/http.d.ts b/types/node/http.d.ts index c516d61d6a..2c9c1e4e52 100644 --- a/types/node/http.d.ts +++ b/types/node/http.d.ts @@ -5,7 +5,7 @@ declare module "http" { import { Socket, Server as NetServer } from "net"; // incoming headers will never contain number - interface IncomingHttpHeaders { + interface IncomingHttpHeaders extends NodeJS.Dict { 'accept'?: string; 'accept-language'?: string; 'accept-patch'?: string; @@ -60,12 +60,10 @@ declare module "http" { 'via'?: string; 'warning'?: string; 'www-authenticate'?: string; - [header: string]: string | string[] | undefined; } // outgoing headers allows numbers (as they are converted internally to strings) - interface OutgoingHttpHeaders { - [header: string]: number | string | string[] | undefined; + interface OutgoingHttpHeaders extends NodeJS.Dict { } interface ClientRequestArgs { @@ -309,7 +307,7 @@ declare module "http" { socket: Socket; headers: IncomingHttpHeaders; rawHeaders: string[]; - trailers: { [key: string]: string | undefined }; + trailers: NodeJS.Dict; rawTrailers: string[]; setTimeout(msecs: number, callback?: () => void): this; /** @@ -358,12 +356,8 @@ declare module "http" { class Agent { maxFreeSockets: number; maxSockets: number; - readonly sockets: { - readonly [key: string]: Socket[]; - }; - readonly requests: { - readonly [key: string]: IncomingMessage[]; - }; + readonly sockets: NodeJS.ReadOnlyDict; + readonly requests: NodeJS.ReadOnlyDict; constructor(opts?: AgentOptions); diff --git a/types/node/os.d.ts b/types/node/os.d.ts index 289b2f3aa7..d98a145f85 100644 --- a/types/node/os.d.ts +++ b/types/node/os.d.ts @@ -46,7 +46,7 @@ declare module "os" { function cpus(): CpuInfo[]; function type(): string; function release(): string; - function networkInterfaces(): { [index: string]: NetworkInterfaceInfo[] }; + function networkInterfaces(): NodeJS.Dict; function homedir(): string; function userInfo(options: { encoding: 'buffer' }): UserInfo; function userInfo(options?: { encoding: string }): UserInfo; diff --git a/types/node/querystring.d.ts b/types/node/querystring.d.ts index 0fd6fee0f2..be1bbf904f 100644 --- a/types/node/querystring.d.ts +++ b/types/node/querystring.d.ts @@ -8,10 +8,9 @@ declare module "querystring" { decodeURIComponent?: (str: string) => string; } - interface ParsedUrlQuery { [key: string]: string | string[]; } + interface ParsedUrlQuery extends NodeJS.Dict { } - interface ParsedUrlQueryInput { - [key: string]: string | number | boolean | string[] | number[] | boolean[] | undefined | null; + interface ParsedUrlQueryInput extends NodeJS.Dict { } function stringify(obj?: ParsedUrlQueryInput, sep?: string, eq?: string, options?: StringifyOptions): string; diff --git a/types/node/repl.d.ts b/types/node/repl.d.ts index 5e321d2718..ef9da3757e 100644 --- a/types/node/repl.d.ts +++ b/types/node/repl.d.ts @@ -146,7 +146,7 @@ declare module "repl" { /** * The commands registered via `replServer.defineCommand()`. */ - readonly commands: { readonly [name: string]: REPLCommand | undefined }; + readonly commands: NodeJS.ReadOnlyDict; /** * A value indicating whether the REPL is currently in "editor mode". * diff --git a/types/node/scripts/generate-inspector/generate-substitute-args.ts b/types/node/scripts/generate-inspector/generate-substitute-args.ts index 95f757f191..b28e3f59c1 100644 --- a/types/node/scripts/generate-inspector/generate-substitute-args.ts +++ b/types/node/scripts/generate-inspector/generate-substitute-args.ts @@ -90,7 +90,7 @@ const createPostFunctions = (command: schema.Command, domain: string): string[] * substituted into ./inspector.d.ts.template. * @param protocol The parsed contents of the JSON file from which the DevTools Protocol docs are generated. */ -export const generateSubstituteArgs = (protocol: schema.Schema): { [propName: string]: string[] } => { +export const generateSubstituteArgs = (protocol: schema.Schema): NodeJS.Dict => { const interfaceDefinitions: string[] = protocol.domains .map(item => { const typePool = (item.types || []).concat(filterNull([ diff --git a/types/node/scripts/generate-inspector/index.ts b/types/node/scripts/generate-inspector/index.ts index 7cb108bdc7..bd51cb9da6 100644 --- a/types/node/scripts/generate-inspector/index.ts +++ b/types/node/scripts/generate-inspector/index.ts @@ -53,7 +53,7 @@ function writeProtocolsToFile(jsonProtocols: string[]) { const template = readFileSync(`${__dirname}/inspector.d.ts.template`, "utf8"); const inspectorDts = substitute(template, substituteArgs).split("\n") - .map(line => trimRight(line)) + .map(trimRight) .join("\n"); writeFileSync("./inspector.d.ts", inspectorDts, "utf8"); diff --git a/types/node/scripts/generate-inspector/utils.ts b/types/node/scripts/generate-inspector/utils.ts index 8b81967b76..f39c4e0bba 100644 --- a/types/node/scripts/generate-inspector/utils.ts +++ b/types/node/scripts/generate-inspector/utils.ts @@ -73,7 +73,7 @@ export const createDocs = ({ deprecated, description, experimental }: Documentab */ export const substitute = ( str: string, - args: { [propName: string]: string[] }, + args: NodeJS.Dict, ): string => { return str.split("\n") .map(line => { @@ -81,11 +81,11 @@ export const substitute = ( const matches = line.match(regex); if (matches) { const [_0, prefix, argName] = matches; - if (args[argName]) { - return args[argName].map(l => prefix + l); - } else { - return []; + const arg = args[argName]; + if (arg) { + return arg.map(l => prefix + l); } + return []; } return [line]; }) diff --git a/types/node/test/module.ts b/types/node/test/module.ts index 42328af8f5..39c78f51f1 100644 --- a/types/node/test/module.ts +++ b/types/node/test/module.ts @@ -37,8 +37,8 @@ const resolved2: string = customRequire2.resolve('test'); const paths1: string[] | null = customRequire1.resolve.paths('test'); const paths2: string[] | null = customRequire2.resolve.paths('test'); -const cachedModule1: Module = customRequire1.cache['/path/to/module.js']; -const cachedModule2: Module = customRequire2.cache['/path/to/module.js']; +const cachedModule1: Module | undefined = customRequire1.cache['/path/to/module.js']; +const cachedModule2: Module | undefined = customRequire2.cache['/path/to/module.js']; const main1: Module | undefined = customRequire1.main; const main2: Module | undefined = customRequire2.main; diff --git a/types/node/test/os.ts b/types/node/test/os.ts index 9f690aacb9..82604ba9cb 100644 --- a/types/node/test/os.ts +++ b/types/node/test/os.ts @@ -39,7 +39,7 @@ import * as os from 'os'; } { - let result: { [index: string]: os.NetworkInterfaceInfo[] }; + let result: NodeJS.Dict; result = os.networkInterfaces(); } diff --git a/types/node/tls.d.ts b/types/node/tls.d.ts index 5712d2f939..a702b4c64f 100644 --- a/types/node/tls.d.ts +++ b/types/node/tls.d.ts @@ -38,7 +38,7 @@ declare module "tls" { subject: Certificate; issuer: Certificate; subjectaltname: string; - infoAccess: { [index: string]: string[] | undefined }; + infoAccess: NodeJS.Dict; modulus: string; exponent: string; valid_from: string; diff --git a/types/node/ts3.5/wasi.d.ts b/types/node/ts3.5/wasi.d.ts index 4a79ecc2ac..ecf31707dc 100644 --- a/types/node/ts3.5/wasi.d.ts +++ b/types/node/ts3.5/wasi.d.ts @@ -17,9 +17,7 @@ declare module 'wasi' { * directories within the sandbox. The corresponding values in `preopens` are * the real paths to those directories on the host machine. */ - preopens?: { - [key: string]: string; - }; + preopens?: NodeJS.Dict; /** * By default, WASI applications terminate the Node.js @@ -49,6 +47,6 @@ declare module 'wasi' { * should be passed as the `wasi_unstable` import during the instantiation of a * [`WebAssembly.Instance`][]. */ - readonly wasiImport: { [key: string]: any }; // TODO: Narrow to DOM types + readonly wasiImport: NodeJS.Dict; // TODO: Narrow to DOM types } } diff --git a/types/node/url.d.ts b/types/node/url.d.ts index d3a395b0c4..152ef5dda1 100644 --- a/types/node/url.d.ts +++ b/types/node/url.d.ts @@ -92,7 +92,7 @@ declare module "url" { } class URLSearchParams implements Iterable<[string, string]> { - constructor(init?: URLSearchParams | string | { [key: string]: string | string[] | undefined } | Iterable<[string, string]> | Array<[string, string]>); + constructor(init?: URLSearchParams | string | NodeJS.Dict | Iterable<[string, string]> | Array<[string, string]>); append(name: string, value: string): void; delete(name: string): void; entries(): IterableIterator<[string, string]>; diff --git a/types/node/util.d.ts b/types/node/util.d.ts index e1507b9c81..30e7e49893 100644 --- a/types/node/util.d.ts +++ b/types/node/util.d.ts @@ -12,9 +12,7 @@ declare module "util" { function inspect(object: any, showHidden?: boolean, depth?: number | null, color?: boolean): string; function inspect(object: any, options: InspectOptions): string; namespace inspect { - let colors: { - [color: string]: [number, number] | undefined - }; + let colors: NodeJS.Dict<[number, number]>; let styles: { [K in Style]: string }; diff --git a/types/node/vm.d.ts b/types/node/vm.d.ts index 7f8ff9c74e..822bd151db 100644 --- a/types/node/vm.d.ts +++ b/types/node/vm.d.ts @@ -1,7 +1,5 @@ declare module "vm" { - interface Context { - [key: string]: any; - } + interface Context extends NodeJS.Dict { } interface BaseOptions { /** * Specifies the filename used in stack traces produced by this script. diff --git a/types/node/worker_threads.d.ts b/types/node/worker_threads.d.ts index 5500283f30..d38a316c0f 100644 --- a/types/node/worker_threads.d.ts +++ b/types/node/worker_threads.d.ts @@ -62,7 +62,7 @@ declare module "worker_threads" { * were passed as CLI options to the script. */ argv?: any[]; - env?: NodeJS.ProcessEnv | typeof SHARE_ENV; + env?: NodeJS.Dict | typeof SHARE_ENV; eval?: boolean; workerData?: any; stdin?: boolean;