microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.29k stars 12.39k forks source link

Static method inheritance #3334

Closed MikeBall0 closed 9 years ago

MikeBall0 commented 9 years ago

There's been some previous discussion about this https://typescript.codeplex.com/workitem/2047 but nothing seems to have come of it. I've got a real scenario where this is getting in the way.

I'm trying to make a .d.ts file for greensock's gsap TweenMax but I have an issue where TweenMax extends TweenLite, which has a static to(target:Object, duration:number, vars:Object):TweenLite, and TweenMax has a static to(target:Object, duration:number, vars:Object):TweenMax.

I can't use the TweenMax.to method because it's being hidden by the TweenLite.to, which makes most of the module useless.

If there's a way to make the definition work as intended I'd love to hear it, otherwise it would be nice to see this fixed.

danquirk commented 9 years ago

For reference on why this behavior exists/persists, see: https://github.com/Microsoft/TypeScript/issues/613 https://github.com/Microsoft/TypeScript/issues/2082

MikeBall0 commented 9 years ago

Yep, I understand why it exists but it would be really disappointing if there is no way to make a .d.ts that properly describes the behavior of TweenMax. I'm not suggesting a change to how Typescript handles inheriting static methods (at least I don't think I am) but it would be nice if there was a way to indicate that that's how the types work in existing Javascript code. I'm just interested in finishing the TweenMax.d.ts file so that I can start using it in my project.

RyanCavanaugh commented 9 years ago

It's always possible to rewrite the classes in terms of their static and instance sides.

See "Class Decomposition" in the TypeScript Handbook http://www.typescriptlang.org/Handbook#writing-dts-files

MikeBall0 commented 9 years ago

Ah, that's a very interesting pattern that I wasn't aware of. I decomposed the class and now things are working, only thing is the autocomplete for TweenMax. shows that all the static methods belong to TweenMax_static, I couldn't figure out if there's a way to have the same name for both the static and instance sides, but it's very minor.

Thanks for the guidance, I'll definitely keep that in mind :)

jeffreymorlan commented 9 years ago
declare class TweenLite {
    static to(target: Object, duration: number, vars: Object): TweenLite;
}
declare class TweenMax extends TweenLite {
    static to(target: Object, duration: number, vars: Object): TweenMax;
}

I don't see any error on this in either 1.4 or 1.5. I think the only way you could get an error on this to override is if there's something else that's making TweenMax not be assignable to TweenLite.

MikeBall0 commented 9 years ago

Hmm... I just tried it again and it seems to be working correctly (though it's yelling that the static side doesn't extend properly, I suspect that's from other problems at this point since a simple example like yours appears to work). That's strange because when I first opened this issue it was definitely insisting that TweenMax.to returned a TweenLite, contrary to the signature. I had multiple people look at it, so I think not crazy and I'm not sure why it's working now. Perhaps it was a project configuration issue on my end...

mhegazy commented 9 years ago

Looks like the issue has been handled. please reopen if there is any other issues.