Improve node module types (#40456)

Add missing APIs on require.resolve from createRequire.
This commit is contained in:
George 2019-11-19 14:05:03 -05:00 committed by Sheetal Nandi
parent 0e9f7eb3a2
commit f4e58f2e65
2 changed files with 24 additions and 4 deletions

View File

@ -193,9 +193,13 @@ interface NodeRequireFunction {
(id: string): any;
}
interface NodeRequireCache {
[path: string]: NodeModule;
}
interface NodeRequire extends NodeRequireFunction {
resolve: RequireResolve;
cache: any;
cache: NodeRequireCache;
/**
* @deprecated
*/
@ -1134,8 +1138,8 @@ declare namespace NodeJS {
/**
* @deprecated Deprecated since: v12.2.0. Please use createRequire() instead.
*/
static createRequireFromPath(path: string): NodeRequireFunction;
static createRequire(path: string): NodeRequireFunction;
static createRequireFromPath(path: string): NodeRequire;
static createRequire(path: string): NodeRequire;
static builtinModules: string[];
static Module: typeof Module;

View File

@ -817,7 +817,23 @@ import moduleModule = require('module');
let paths: string[] = module.paths;
paths = m1.paths;
moduleModule.createRequireFromPath('./test')('test');
const customRequire1 = moduleModule.createRequireFromPath('./test');
const customRequire2 = moduleModule.createRequire('./test');
customRequire1('test');
customRequire2('test');
const resolved1: string = customRequire1.resolve('test');
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 main1: Module | undefined = customRequire1.main;
const main2: Module | undefined = customRequire2.main;
}
/////////////////////////////////////////////////////////