Static members

We can use the static keyword to enable the usage properties and methods in a class without needing to create an instance of it:

class TemperatureConverter { 
 
    public static CelsiusToFahrenheit( 
        celsiusTemperature: number 
    ) { 
        return (celsiusTemperature * 9 / 5) + 32; 
    } 
 
    public static FahrenheitToCelsius( 
        fahrenheitTemperature: number 
    ) { 
        return (fahrenheitTemperature - 32) * 5 / 9; 
    } 
 
} 

As we can observe in the preceding code snippet, the TemperatureConverter class has two static methods named CelsiusToFahrenheit and FahrenheitToCelsius. We can invoke these methods without creating an instance of the TemperatureConverter class:

let fahrenheit = 100; let celsius = TemperatureConverter.FahrenheitToCelsius(fahrenheit); fahrenheit ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.