-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't augment a module from global scope #17736
Comments
I am facing the same issue with lodash module where i can't augment it (microsoft/types-publisher#367) |
@mhegazy sorry for pinging you but could you please give some feedback on any progress on this one? This one blocks us from using several updated typings. |
The only way I was able to get this to work with was to create a export {}; // this file needs to be a module
declare module "lodash" {
interface LoDashStatic {
addedToLoDash(): void;
}
} The augmented _.addedToLoDash();
const chain = _.chain; |
Thank you this is a nice and elegant idea, even has an extra level of separation of concerns. I'm going to try this soon and give some feedback. |
you can do something like: declare module "mod" {
module "lodash" {
interface LoDashStatic {
addedToLoDash(): void;
}
}
} i think we need to revisit our augmentation vs. re declaration implementation for modules. |
@aluanhaddad Unfortunately your idea doesn't work in my case. I need to augment a module's root exported namespace and one of the interfaces inside. To be specific, the library which I'm trying to augment is I tried to create a
|
That is almost correct but the the reason it doesn't work as you expect is that the merged declaration in function videojs(id: any, options: videojs.PlayerOptions, ready?: () => void): videojs.Player;
namespace videojs {...} is exported as the value of the module itself. This means that you need to remove the export { }
declare module "videojs" {
function getComponent(name: string): any;
function registerComponent(name: string, definition: any);
function extend(target: any, source: any);
function plugin(name: string, constructor: new (...args: any[]) => any);
interface Player {
loadVideo(videoData: any);
}
} I use the same technique in the LoDash example actually, since This can be a bit confusing. |
Ah I see now, thank you very much for the clarification. Yes this is a bit confusing indeed. Removing the |
EDIT: yeah this is mostly covered by #18877 and co This whole thing is weird... // ts3dutils.d.ts
export interface V3 {
plus(x: V3): V3
}
export as namespace ts3dutils
// ts3dutilsaugment.d.ts
export {}
declare module "./ts3dutils" {
interface V3 {
__magic_type: V3
}
}
// index.ts
/// <reference path="ts3dutils.d.ts" />
/// <reference path="ts3dutilsaugment.d.ts" />
type A = ts3dutils.V3['plus'] // OK!
type X = ts3dutils.V3['__magic_type'] // OK! BUT if the interface exported as namespace is actual reexported from somewhere else: // V3.d.ts
export interface V3 {
plus(x: V3): V3
}
// ts3dutils.d.ts
export * from './V3'
export as namespace ts3dutils
// ts3dutilsaugment.d.ts
export {}
declare module "./ts3dutils" {
interface V3 {
__magic_type: V3
}
}
// index.ts
/// <reference path="ts3dutils.d.ts" />
/// <reference path="ts3dutilsaugment.d.ts" />
type A = ts3dutils.V3['plus'] // OK!
// ERROR: [ts] Property '__magic_type' does not exist on type 'V3'.
type X = ts3dutils.V3['__magic_type'] Related to #9681 (?) because the underlying issue seems to be that What I was originally trying to do was hack in conditional mapped types. I.e. augment other types so I can do |
Augmentations have to target the physical module that the export originates from. |
@mhegazy your solution did work for me: declare module "mod" {
module "lodash" {
interface LoDashStatic {
addedToLoDash(): void;
}
}
} Although I don't really understand what |
the name |
Personally, I find // augmentations.ts
export {} // ensure this is a module
declare module "lodash" {
interface LoDashStatic {
addedToLoDash(): void;
}
} easier to read. |
Talked about this with @mhegazy and we couldn't think of a good solution to this besides @aluanhaddad's. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
@cervengoc Can you provide a sample of consuming the custom video.js component?
|
I'm having a specific typings file which has a modern shape like this.
I'm trying to consume this module in file which is not a module, so the official module augmentation doesn't work. My task would be to add another function to the
something
namespace root, and another to theA
interface.I've tried to utilize namespace merging, but it seems like it's not working because instead of merging it thinks that my declaration is the only one, so the original stuff coming from the typings file is not visible at all.
As far as the interface augmentation goes, I've also tried the silly syntax like
But of course that's a syntax error so no way it could work, but it represents half of my goal quite well.
I've put this as a question onto SO but I got no answer nor a comment until now. (https://stackoverflow.com/questions/45505975/how-to-augment-a-module-in-global-context)
TypeScript Version: 2.4.2
Expected behavior:
The module can be augmented from global scope.
Actual behavior:
The augmentation actually kind of overwrites the entire module namespace.
The text was updated successfully, but these errors were encountered: