TypeScript: Cannot selectively include types in a declares from a modular node package.
I’ve bounced this around StackOverflow without any luck, so it may be something that declares files are currently unable to handle… I am trying to create a declares file for a node package, let’s call it simply package. This package has different modules that define methods on a single core object. The structure looks like this:
// index.js
require('./foo');
require('./bar');
module.exports = require('./core');
// foo/index.js
var obj = require('../core');
obj.foo_method_1 = require('./foo_method_1');
obj.foo_method_2 = require('./foo_method_2');
// etc
module.exports = obj;
// bar/index.js
var obj = require('../core');
obj.bar_method_1 = require('./bar_method_1');
obj.bar_method_2 = require('./bar_method_2');
// etc
module.exports = obj;
When requiring in node, using require('package'), require('package/foo'), and require('package/bar') all return the same object, however the last 2 will only define the methods within that module, while the first will define methods in all modules.
The goal is to be able structure the declares in a way that will model this, with only types for foo defined when using import * as package from 'package/foo';. However, the problem I keep hitting is that no matter how I structure it (I’ve tried several approaches), typescript will always end up loading the root declares file (package/index.d.ts) so that all members exist on the object, when this may not be the case.
How can I structure the declares so that only certain members exist when importing sub-modules?
Any help is appreciated. Thank you.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (10 by maintainers)
It would be helpful to add a section to http://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html with this example.