4

I have definition for some interfaces and their implementations. There are a lot of methods that have to be declared on each of the implementing classes.

I find it tedious and redundant as its only a definition. Was there just a lack of time to make this feature happen or some idea behind why ambient implementation definition should be enforced? Or is there something I have missed?

UPDATE

I dislike my question now, it was written from the perspective of a person who is sure the interface members were implemented because the library owner said that. But if I would decide to create my own interface to some other person's library I would be better off forced specifying every implementing member as a sanity check.

rgripper
  • 1,066
  • 15
  • 23

2 Answers2

3

Let's say you didn't have to write out the interface members:

class Base { }
class Derived extends Base { }

interface Foo {
    method(t: number): Base;    
}

declare class FooImpl1 implements Foo {
    // Empty
}

declare class FooImpl2 implements Foo {
    public method(): Derived;
}

Is FooImpl2 trying to declare an additional overload of method, or is FooImpl2 implementing method using a signature that takes fewer parameters and returns a more derived type? Either would be a valid interpretation. You'd have to make rules for all sorts of cases like this so the programmer could specify what they actually meant, making the language less predictable.

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
  • I've never seen any `declare class`, `declare` isn't reserved for `module`? `declare module A{class AB{}}` – Vadorequest Nov 15 '14 at 13:42
  • 1
    I don't understand your arguments. For one the `Empty` case is the most common case and unambiguous as far as I can tell. And your argument for the special cases is not any different with extending classes, where you don't have to write out the base members, either. As far as I can see in your example it doesn't matter whether `FooImpl2` does one or the other - it's a declaration meaning that as a user you can call both methods (the one that is declared explicitly and the one that is implemented implicitly). – Sebastian Oct 30 '15 at 09:13
1

You shouldn't have to give any implementation for an ambient declaration.

For example, an interface would describe only the types with no implementation:

interface MyInterface {
    property: string;
    method(input: number): void;
}

And the same applies for an ambient declaration of a class or module:

declare class MyClass {
    property: string;
    method(input: number): void;
}

If you want to represent an ambient declaration for a class that implements and interface, you can use the following shortcut:

interface MyInterface {
    property: string;
    method(input: number): void;
}

declare var MyClass: MyInterface;
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 2
    I was talking about requirement to declare every member of some ambient interface in every ambient class that implement it. – rgripper Jan 29 '13 at 13:01
  • 2
    I think I understand what you mean - additional example added. The only time this doesn't work is if you expect to extend `MyClass` with a real TypeScript class. – Fenton Jan 29 '13 at 14:23