Skip to content

Latest commit

 

History

History
25 lines (17 loc) · 608 Bytes

constants-in-typescript.md

File metadata and controls

25 lines (17 loc) · 608 Bytes

Constants In TypeScript

Category: TypeScript

Constants in TypeScript should be contained in classes which have the sole purpose of defining constant values.

Create a constants.ts file which declares your constants:

export abstract class Constants {
  static readonly MAX_VALUE: number = 14703;
  //...
}

Important the constants file and use the constant value:

import { Constants } from './constants';

//...

// Obtain a random number between 0 and the max value defined by the constant
const randomNumber = Math.floor(Math.random() * Constants.MAX_VALUE);