No. Interfaces don't exist at runtime, so there's no way to add runtime code such as a method implementation. If you post more specifics about your use case, a more specific or helpful answer may be possible.
EDIT:
Ah, you want multiple inheritance which you can't do with classes in JavaScript. The solution you are probably looking for is mixins.
Adapting the example from the handbook:
class A {
somePropertyA: number;
someFunctionA() {
console.log(this.somePropertyA);
}
}
class B {
somePropertyB: string;
someFunctionB() {
console.log(this.somePropertyB);
}
}
interface C extends A, B {}
class C {
// initialize properties we care about
somePropertyA: number = 0;
somePropertyB: string = 'a';
}
applyMixins(C, [A, B]);
const c = new C();
c.someFunctionA(); // works
c.someFunctionB(); // works
// keep this in a library somewhere
function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
});
});
}
That should work for you. Maybe eventually this can be done less painfully with decorators, but for now the above or something like it is probably your best bet.
Answer from jcalz on Stack OverflowNo. TypeScript Interfaces are not something available at runtime, that is they are completely absent in the generated JavaScript.
Perhaps you meant to use class:
class Test{
imp(){return 123}
}
Your needs will be fulfilled by abstract classes
abstract class Department {
constructor(public name: string) {
}
printName(): void {
console.log("Department name: " + this.name);
}
abstract printMeeting(): void; // must be implemented in derived classes
}
class AccountingDepartment extends Department {
constructor() {
super("Accounting and Auditing"); // constructors in derived classes must call super()
}
printMeeting(): void {
console.log("The Accounting Department meets each Monday at 10am.");
}
generateReports(): void {
console.log("Generating accounting reports...");
}
}
Credits https://www.typescriptlang.org/docs/handbook/classes.html#:~:text=Abstract%20classes%20are%20base%20classes,methods%20within%20an%20abstract%20class.
For more info https://www.typescriptlang.org/docs/handbook/classes.html#:~:text=Abstract%20classes%20are%20base%20classes,methods%20within%20an%20abstract%20class.
Replacing Inheritance with default interface implementation
Interface Default Values
Typescript interface default values - Stack Overflow
Defaulting Unspecified Interface Properties to Null in TypeScript - Ask a Question - TestMu AI Community
I have been thinking about inheritance vs composition a bit lately. A key point of inheritance is implicit reuse.
Lets say we want to extend the behavior of a function on a class A. In a language with inheritance, it is trivial.
class A {
func doSomething() { }
}
class Ae extends A {
func doSomething() {
super.doSomething();
// we do something else here
}
}With composition and an interface we, embed A within Ae and call the method and, within expectation, have the same result
interface I {
func doSomething();
}
class A implements I {
func doSomething() { }
}
class Ae implements I {
A a;
func doSomething() {
a.doSomething();
// do someting else
}
}This works great... until we run into issues where the interface I is long and in order to implement it while only modifying one method, we need to write boiler plate in Ae, calling A as we go explicitly. Inheritance eliminates the additional boiler plate (there may be other headaches including private data fields and methods, etc, but lets assume the extension does not need access to that).
Idea: In order to eliminate the need to explicit inheritance, we add language level support for delegates for interfaces.
interface I {
func doSomething();
func doSomething2();
}
class A implements I {
func doSomething() { }
func doSomething2() { }
}
// All methods in I which are not declared in Ae are then delegated to the
// variable a of type A which implements I.
class Ae implements I(A a) {
func doSomething() {
a.doSomething();
// do someting else
}
// doSomething2 already handled.
}We achieve the reuse of inheritance without an inheritance hierarchy and implicit composition.
But this is just inheritance?
Its not though. You are only allowed to use as a type an interface or a class, but not subclass from another class. You could chain together composition where a "BASE" class A implements I. Then is modifed by utilizing A as the default implementation for class B for I. Then use class B as default implementation for class C, etc. But the type would be restricted into Interface I, and not any of the "SUB CLASSES". class B is not a type of A nor is class C a type of B or A. They all are only implementing I.
Question:
Is this worth anything or just another shower thought? I am currently working out ideas on how to minimize the use of inheritance over composition without giving up the power that comes from inheritance.
On the side where you need to now forward declare the type as an interface and then write a class against it, there may be an easy way to declare that an interface should be generated from a class, which then can be implemented like any other interface as a language feature. This would add additional features closer to inheritance without inheritance.
Why am I against inheritance?
Inheritance can be difficult? Interfaces are cleaner and easier to use at the expense of more code? Its better to write against an Interface than a Class?
Edit 1:
Both-Personality7664 asked regarding how internal function dependencies within the composed object would be handled.
A possible solution would be how the underlying dispatching works. With a virtual table implementation, the context being handled with the delegate would use a patched virtual table between the outer object and the default implementation. Then the composing object call the outer objects methods instead of its own.
// original idea result since A.func1() calling func2() on A would simply call A.func2()
Ae.func1() -> A.func1() -> A.func2()
// updated with using patched vtable // the table would have the updated methods so we a dispatch on func2() on A would call Ae with func2() instead of A. Ae.func1() -> A.func1() -> Ae.func2()
Edit 2:
Mercerenies pointed out Kotlin has it.
It seems kotlin does have support for this, or at least part of it.
Can I tell the interface to default the properties I don't supply to null? What would let me do this
No. You cannot provide default values for interfaces or type aliases as they are compile time only and default values need runtime support
Alternative
But values that are not specified default to undefined in JavaScript runtimes. So you can mark them as optional:
interface IX {
a: string,
b?: any,
c?: AnotherType
}
And now when you create it you only need to provide a:
let x: IX = {
a: 'abc'
};
You can provide the values as needed:
x.a = 'xyz'
x.b = 123
x.c = new AnotherType()
You can't set default values in an interface, but you can accomplish what you want to do by using optional properties:
Simply change the interface to:
interface IX {
a: string,
b?: any,
c?: AnotherType
}
You can then do:
let x: IX = {
a: 'abc'
}
And use your init function to assign default values to x.b and x.c if those properies are not set.