-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Extend/augment static member of class #39870
Comments
You can only add signatures via augmentation (this is why we call it augmentation). |
So redefining of signature of a static method is not possible? Is it a current limitation or there is a valid reason for this? |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
@whitecolor Redefinition isn't possible; see e.g. #36146 |
@RyanCavanaugh Sorry to revisit this old thread but I think I have the same issue and it wasn't solved here. Even if @wclr used the word redefining he has been always thinking about augmentation. Take a new look to the provided example in the OP. The problem is that if you have a class with an static method, you cannot augment it as you would with instance methods. Look: class Foo {
static foo() { }
}
declare namespace Foo {
export function foo(n: 1): void;
} This fails with Is there any workaround? Is this behavior expected (and why)? Should I open another issue? Should we take a lot of coffee and try to rewrite TypeScript in Perl? Thank you. Hope you see this message. |
@InExtremaRes this is how to do that: index.d.tsinterface Foo {
}
interface FooConstructor {
new (): Foo;
foo(n: number): void
readonly "prototype": Foo;
}
declare var Foo: FooConstructor; In JavaScript, you declare your class Foo {
constructor() {
}
static foo(n) {
console.log(`foo ${n}`);
}
} Now let's say you want to add a new static method to Foo, you can add the following to the same interface FooConstructor {
bar(s: string): void
} In a regular if (!Foo.bar) {
Foo.bar = function(s: string): void {
console.log(`bar: ${s}`);
}
} Note that if you had just written |
TypeScript Version: 3.7.x-dev.201xxxxx
Search Terms:
This is probably kind of question, asked it on SO, but din't get the answer.
I have an external module called
some-module
that definesSome
class:want to redefine the signature of some methods for my project, so I create
.d.ts
file in my project:But I need to redefine the signature of static
create
method, so I tried to do it usingnamespace
:Then
Expected behavior:
The new signature would be added to the existing static method.
Actual behavior:
Signature in
namespace
for the existing method has no effect.The text was updated successfully, but these errors were encountered: