Is it possible to declare a class in one file and define its methods in separate files?
I have some classes with a lot of methods and it would be great if I could spread them out a bit.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Short answer: Typescript doesn’t support splitting up a class definition into several files.
Workaround: You could define an interface containing members for the class, and two different classes implementing that interface. Then mixin properties from one class to the other, to make a combined class. For example:
LargeClass.a.ts
LargeClass.b.ts
Usage.ts
This won’t work if you need constructors for the class, though. And really it’s suboptimal… Workaround none the less 🙂
Oh, by the way, this works because typescript doesn’t emit unitialised property/field type declarations for classes–it only uses them for type checking.
I also realise that you can do this without interfaces and just the class construct in a prettier way… I’ll leave how to do that as an exercise to readers for now…