Why does a Typescript frontend implementation have to be declared?

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

I find this tedious and redundant as this is the only definition. Was there just a lack of time for this feature to happen or some idea why there should be forced ambient detection? Or am I missing something?

UPDATE

I don't like my question right now, it was written from the point of view of someone who is sure the interface members were implemented because the owner of the library said so. But if I decided to create my own interface for some other library, I would be better off enforcing each executor as a health check.

+3


source to share


2 answers


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 it FooImpl2

an attempt to declare an additional overload method

or FooImpl2

implementation method

using a signature that takes fewer parameters and returns a more derived type? Any of these would be a valid interpretation. You will need to create rules for all such cases so that the programmer can specify what they really mean, making the language less predictable.

+3


source


You don't need to give any implementation for the ambient declaration.

For example, an interface will only describe types without implementation:

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

      

And the same applies to an ambient class or module declaration:



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

      

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

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

declare var MyClass: MyInterface;

      

+1


source







All Articles