-
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
Issues with extending ObjectConstructor interface #3889
Comments
This is due to some limitations in definition merging where interfaces declared in Your example would have worked if you could say
but you can't do The only solution is to move the |
@billccn interfaces always merge, even accross files. there is no such limitation. looks like you are talking about interfaces defined within a module, since modules have thier own scope, and interface with the same name as one in the global scope will not merge.. e.g.: // global.d.ts
interface I {
a: string
}
// myModule.ts
import d from "anotherModule";
// This I is local to myModule and does not merge with the global"
interface I {
b: number;
} |
@kitsonk the sample you provided works fine for me on latest. can you provide more details? |
Ok, I tried several different variations because I got it "working" accidentally and I realise the problem. Now I don't know if it is as designed or not (it is very confusing from a developer though). The following works: interface ObjectConstructor {
assign(target: any, ...sources: any[]): any;
}
let assign = Object.assign ? Object.assign : function(target: any, ...sources: any[]): any {};
function test () {}; The following gives interface ObjectConstructor {
assign(target: any, ...sources: any[]): any;
}
let assign = Object.assign ? Object.assign : function(target: any, ...sources: any[]): any {};
export function test () {}; Obviously it has to do with modules and namespacing, but I will admit I am confused. |
When you have a top-level This is intentional because otherwise there wouldn't be any way to make a new interface local to your module that had the same name as a global type. |
Thanks. I figured it was intentional, but obviously it is rather confusing, especially when not having a "choice" about accessing globally scoped namespaces at the implementation level. I assume there are no language semantics to denote that I am referring to the global namespace? Therefore, the only way to extend it is as @billccn suggests (and @mhegazy implies) by declaring in a non-module, like a project |
@kitsonk Given what @RyanCavanaugh explained, could you do something like this? interface ObjectCtor extends ObjectConstructor {
assign(target: any, ...sources: any[]): any;
}
declare var Object: ObjectCtor;
export let assign = Object.assign ? Object.assign : function(target: any, ...sources: any[]): any {
return;
}; |
@bryanforbes thanks for the effective workaround. #983 being re-opened would also address the "limitation" I am perceiving so I will close in lieu of that. |
Guys, sorry to comment in this zombie thread but there is weird stuff happening. // file polyfills.d.ts
declare interface ObjectContructor {
assign(target: any, ...sources: any[]): any;
} but declaring it inside global, using module augmentation works // polyfill.ts
declare global {
export interface ObjectContructor {
assign(target: any, ...sources: any[]): any;
}
}
Object.assign = ... Any idea why this is? |
i would assume that the file using the |
While this currently doesn't error in Playground, in 1.5.0-beta and release-1.5 branch, I get
error TS2339: Property 'assign' does not exist on type 'ObjectConstructor'
for the following:The text was updated successfully, but these errors were encountered: