Ehsan Ghanbari

Experience, DotNet, Solutions

Interface declarations in TypeScript

TypeScript allows us to define complex types via interfaces. Interfaces play a very important role in complex scenarios. for example when objects contain other properties. In regular form, defining an interface is a little bit like the C# syntax and implementing an interface member by a class is in the below form:

interface IBaseInterface {



You can define generic types in TypeScript like C# as well: 

interface IFoo2<T, U> {

    Action<T, U>();


interface IFoo3<T, U> {

    Action<T, U>(): IFoo2<T, U>;


Defining an output of the methods in interfaces and also the type of properties are very cool:

interface IMachine {

    SpeedTest(): string;

    Color: string;


And in the case of implementing the IMachine members you have to return a value (string in this case) in SpeedTest():

class Machine implements IMachine {

    Color: string;

    public SpeedTest() {

        return "String!";


Notes about interfaces:

  1. Interfaces are purely compiled time construct
  2. Are useful for documenting and validating the required shape of properties.
  3. An interface may optionally have a type parameter
  4. An interface with type parameter is called generic interfaces.
  5. An interface cannot declare a property with the same name as an inherited private property.

Introducing Typescript

There is a fact that writing application in JavaScript is so hard. JavaScript is not a programming language and this makes it so hard to manipulate it especially in large application and JavaScript is never designed for large application development. Typescript is a language for application-scale JavaScript development, free and open source programming language by Microsoft which compiles to plain JavaScript. Typescript is entirely dynamically typed and every JavaScript code is a valid typescript code and you can copy and Paste JavaScript code to typescript. Typescript supports tools for large-scale JavaScript applications for any browser, for any host on any OS.

As you can see in the example below (created by the visual studio), Typescript adds optional types, classes, and modules to JavaScript. and essentially adds optional static typing and class-based object-oriented programming.


// Interface
interface IPoint {
    getDist(): number;

// Module
module Shapes {
    // Class
    export class Point implements IPoint {
        // Constructor
        constructor (public x: number, public y: number) { }

        // Instance member
        getDist() { return Math.sqrt(this.x * this.x + this.y * this.y); }

        // Static member
        static origin = new Point(0, 0);

// Local variables
var p: IPoint = new Shapes.Point(3, 4);
var dist = p.getDist();


Typescript extends JavaScript syntax, so any existing JavaScript programs work with Typescript without any changes. Typescript is designed for development of large applications and when translated it produces JavaScript to ensure compatibility.

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to query over Icollection<> of a type with linq
How to use PagedList In MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
Using Generic type for type casting in F#