Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript default function inside interface

Tags:

typescript

This is working

interface test{
    imp():number;
}

but it is possible to implement a function inside.

interface test{
    imp():number{
      // do something if it is not overwritten
    }
}

This is not working in typescript for me, but there may be some reserved word, for example default or similar, to implement a function inside an interface that is not overwritten if the default work.

like image 523
Angel Angel Avatar asked Mar 20 '16 04:03

Angel Angel


People also ask

How do you define a function inside an interface in TypeScript?

TypeScript Interface can be used to define a function type by ensuring a function signature. We use the optional property using a question mark before the property name colon. This optional property indicates that objects belonging to the Interface may or may not have to define these properties.

Can a TypeScript interface have default values?

In TypeScript, interfaces represent the shape of an object. They support many different features like optional parameters but unfortunately do not support setting up default values.

How do you make properties optional in interface TypeScript?

Use the Partial utility type to make all of the properties in a type optional, e.g. const emp: Partial<Employee> = {}; . The Partial utility type constructs a new type with all properties of the provided type set to optional. Copied!

How do I inherit an interface in TypeScript?

Interfaces and Inheritance An interface can be extended by other interfaces. In other words, an interface can inherit from other interface. Typescript allows an interface to inherit from multiple interfaces. Use the extends keyword to implement inheritance among interfaces.


2 Answers

No. 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}
 }
like image 77
basarat Avatar answered Oct 12 '22 10:10

basarat


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.

like image 35
Chetan Jain Avatar answered Oct 12 '22 10:10

Chetan Jain