Typescript: constants in an interface

Issue

How do I place a constant in an Interface in typescript. Like in java it is:

interface OlympicMedal {
  static final String GOLD = "Gold";
  static final String SILVER = "Silver";
  static final String BRONZE = "Bronze";
}

Solution

You cannot declare values in an interface.

You can declare values in a module:

module OlympicMedal {
    export var GOLD = "Gold";
    export var SILVER = "Silver";
}

In an upcoming release of TypeScript, you will be able to use const:

module OlympicMedal {
    export const GOLD = "Gold";
    export const SILVER = "Silver";
}

OlympicMedal.GOLD = 'Bronze'; // Error

Answered By – Ryan Cavanaugh

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.