2

It's not possible to add class local interfaces and type declarations like so:

class Foo {
  interface Bar {}
  private bar: Bar;
}

Up till now, I've been putting everything inside a module:

module Foo {
  export interface Bar {}
  export class Foo {
    private bar: Bar;
  }
}

However, I recently found out that you can create class/module "hybrids" so to speak.

class Foo {
  private bar: Foo.Bar;
}

module Foo {
  export interface Bar {}
}

The compiler does not complain about this and the type checker seems to do everything right. Is this a bad idea? Will it break in the future?

2 Answers 2

2

This language feature is outlined in the documentation:

Declaration Merging: Merging Modules with Classes, Functions, and Enums

Considering that it's outlined in the documentation and that this pattern is fairly common (especially in existing JavaScript libraries), I think it's safe to say this language feature is here to stay.

Sign up to request clarification or add additional context in comments.

Comments

1

This is a well-documented feature called "Declaration Merging", so it is expected to work stably for a while.

Specifically, any content in the module that can generate code will simply become static members of the class and interfaces do not generate code anyways so it does not matter if they exist in classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.