Skip to content

Commit

Permalink
Improve node module types (#40456)
Browse files Browse the repository at this point in the history
Add missing APIs on require.resolve from createRequire.
  • Loading branch information
GeorgeTaveras1231 authored and sheetalkamat committed Nov 19, 2019
1 parent 0e9f7eb commit f4e58f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
10 changes: 7 additions & 3 deletions types/node/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 17 additions & 1 deletion types/node/node-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/////////////////////////////////////////////////////////
Expand Down

0 comments on commit f4e58f2

Please sign in to comment.