chore(node): mark object values as undefine-able (#43931)

This commit is contained in:
Simon Schick 2020-04-17 14:05:14 -07:00 committed by GitHub
parent 957cbafa10
commit f7e28122e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 42 additions and 57 deletions

View File

@ -133,7 +133,7 @@ declare module "child_process" {
uid?: number;
gid?: number;
cwd?: string;
env?: NodeJS.ProcessEnv;
env?: NodeJS.Dict<string>;
}
interface CommonOptions extends ProcessEnvOptions {

View File

@ -100,9 +100,7 @@ declare module "cluster" {
settings: ClusterSettings;
setupMaster(settings?: ClusterSettings): void;
worker?: Worker;
workers?: {
[index: string]: Worker | undefined
};
workers?: NodeJS.Dict<Worker>;
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<Worker>;
/**
* events.EventEmitter

View File

@ -669,9 +669,8 @@ declare namespace NodeJS {
isTTY?: true;
}
interface ProcessEnv {
[key: string]: string | undefined;
}
// Alias for compability
type ProcessEnv = Dict<string>;
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<string>;
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<NodeModule>;
/**
* @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<T> {
[key: string]: T | undefined;
}
interface ReadOnlyDict<T> {
readonly [key: string]: T | undefined;
}
}

16
types/node/http.d.ts vendored
View File

@ -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<string | string[]> {
'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<number | string | string[]> {
}
interface ClientRequestArgs {
@ -309,7 +307,7 @@ declare module "http" {
socket: Socket;
headers: IncomingHttpHeaders;
rawHeaders: string[];
trailers: { [key: string]: string | undefined };
trailers: NodeJS.Dict<string>;
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<Socket[]>;
readonly requests: NodeJS.ReadOnlyDict<IncomingMessage[]>;
constructor(opts?: AgentOptions);

2
types/node/os.d.ts vendored
View File

@ -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<NetworkInterfaceInfo[]>;
function homedir(): string;
function userInfo(options: { encoding: 'buffer' }): UserInfo<Buffer>;
function userInfo(options?: { encoding: string }): UserInfo<string>;

View File

@ -8,10 +8,9 @@ declare module "querystring" {
decodeURIComponent?: (str: string) => string;
}
interface ParsedUrlQuery { [key: string]: string | string[]; }
interface ParsedUrlQuery extends NodeJS.Dict<string | string[]> { }
interface ParsedUrlQueryInput {
[key: string]: string | number | boolean | string[] | number[] | boolean[] | undefined | null;
interface ParsedUrlQueryInput extends NodeJS.Dict<string | number | boolean | string[] | number[] | boolean[] | null> {
}
function stringify(obj?: ParsedUrlQueryInput, sep?: string, eq?: string, options?: StringifyOptions): string;

View File

@ -146,7 +146,7 @@ declare module "repl" {
/**
* The commands registered via `replServer.defineCommand()`.
*/
readonly commands: { readonly [name: string]: REPLCommand | undefined };
readonly commands: NodeJS.ReadOnlyDict<REPLCommand>;
/**
* A value indicating whether the REPL is currently in "editor mode".
*

View File

@ -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<string[]> => {
const interfaceDefinitions: string[] = protocol.domains
.map(item => {
const typePool = (item.types || []).concat(filterNull([

View File

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

View File

@ -73,7 +73,7 @@ export const createDocs = ({ deprecated, description, experimental }: Documentab
*/
export const substitute = (
str: string,
args: { [propName: string]: string[] },
args: NodeJS.Dict<string[]>,
): 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];
})

View File

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

View File

@ -39,7 +39,7 @@ import * as os from 'os';
}
{
let result: { [index: string]: os.NetworkInterfaceInfo[] };
let result: NodeJS.Dict<os.NetworkInterfaceInfo[]>;
result = os.networkInterfaces();
}

2
types/node/tls.d.ts vendored
View File

@ -38,7 +38,7 @@ declare module "tls" {
subject: Certificate;
issuer: Certificate;
subjectaltname: string;
infoAccess: { [index: string]: string[] | undefined };
infoAccess: NodeJS.Dict<string[]>;
modulus: string;
exponent: string;
valid_from: string;

View File

@ -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<string>;
/**
* 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<any>; // TODO: Narrow to DOM types
}
}

2
types/node/url.d.ts vendored
View File

@ -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<string | string[]> | Iterable<[string, string]> | Array<[string, string]>);
append(name: string, value: string): void;
delete(name: string): void;
entries(): IterableIterator<[string, string]>;

View File

@ -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
};

4
types/node/vm.d.ts vendored
View File

@ -1,7 +1,5 @@
declare module "vm" {
interface Context {
[key: string]: any;
}
interface Context extends NodeJS.Dict<any> { }
interface BaseOptions {
/**
* Specifies the filename used in stack traces produced by this script.

View File

@ -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<string> | typeof SHARE_ENV;
eval?: boolean;
workerData?: any;
stdin?: boolean;