Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8632089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:18:17+00:00 2026-06-12T09:18:17+00:00

Is it possible to declare a class in one file and define its methods

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:18:18+00:00Added an answer on June 12, 2026 at 9:18 am

    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

    interface LargeClass {
       methodA(): string;
       methodB(): string;
    }
    
    class LargeA implements LargeClass {
       methodA: () => string; // not implemented, needed since otherwise we don't extend LargeClass
       methodB() {
         return "Hello world";
       }
    }
    

    LargeClass.b.ts

    class LargeB implements LargeClass {
       methodA() {
         return "Foo";
       }
       methodB: () => string; // not implemented, needed since otherwise we don't extend LargeClass
    }
    

    Usage.ts

    // Using underscore's extend to copy implementation from A to B
    var c:LargeClass = _.extend(new LargeA(), new LargeB());
    
    // Manually mixing in a to b
    var a = new LargeA();
    var b:LargeClass = new LargeB();
    for (var prop in a) {
        b[prop]=a[prop];
    }
    

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions. I know it is possible to declare class objects in
Possible Duplicate: Why would one declare a Java interface method as abstract? The following
Is it possible to forward-declare a nested class, then use it as the type
Is it possible to declare more than one variable using a with statement in
I have the following situation: Three concrete service classes implement a service interface: one
I have a framework library which would do a lot of things including logging.
I have this weird situation. I have these two classes: Public Class Entry End
Is it possible to declare class variable outside of if-else and initialize it inside
Is it possible to initialize objects of one class in the constructor of another
Is it possible to make a C++ header file (.h) that declares a class,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.