38

I have bunch of constants which are business related and I have to store them in Angular.Something like below

STUDENT_NAMES= ["JOHN","BOB","NICK"]
TEACHER_NAME = ["HARRY","CHRIS"]
 SCHOOL_CODE = [100,102,107];

I have a lot of them and they are kind of static ..I need them in most of my service classes. What is the best place to store them? Should i create a interface and let my service class inherit them? In java , we define a class of public static final constants and other classes uses them.Like this , what is the typescript way?

5 Answers 5

69

Define an abstract class for your constants:

export abstract class Constants {
  static readonly STUDENT_NAMES = ["JOHN", "BOB", "NICK"];
  static readonly TEACHER_NAME = ["HARRY", "CHRIS"];
  static readonly SCHOOL_CODE = [100, 102, 107];
}

(Edit: Types string[] and number[] are inferred by TS)

Then include this class whereever needed with import { Constants } from '...'; and use its values with const names: string[] = Constants.STUDENT_NAMES;

Regarding the naming I agree with @AdrianBrand to prefer names like studentNames, teacherNames and schoolCodes.

Edit: TypeScript 3.4 introduced so called const assertions, which might also be suited:

export const constants = {
  studentNames: ["JOHN", "BOB", "NICK"],
  ...
} as const;
3
  • 1
    Best way to create constants. Thanks. Commented Apr 29, 2021 at 16:15
  • abstract not really needed though imo - out of interest why use abstract here? Shoudnt devs update the source instead of adding more constants elsewere?
    – dalcam
    Commented Dec 9, 2022 at 0:07
  • 2
    @dalcam Yes, abstract is indeed not needed per se. But I like the idea not being able to instantiate Constants.
    – pzaenger
    Commented Dec 11, 2022 at 13:41
22

You put them in a TypeScript file and import them where needed.

export const STUDENT_NAMES: string[] = ["JOHN","BOB","NICK"];

Put it in student-names.ts

import { STUDENT_NAMES } from './path/cosnstants/student-names';

where you need it.

Personally I would name them studentNames and not STUDENT_NAMES but that is a matter of taste.

10

You can create a TS file constants.ts that contains all your constants :

export const constants = {
   STUDENT_NAMES: ["JOHN","BOB","NICK"],
   TEACHER_NAME: ["HARRY","CHRIS"],
   SCHOOL_CODE: [100,102,107]
};

Then whenever you need a constant you can call it like this code bellow:

let studentsNames : any = constants.STUDENT_NAMES;

Regards,

1
  • 4
    But then you are not explicitly declaring the types of the constants, so it isn't very typescripty, and they aren't constant - they can be reassigned.
    – Tom
    Commented Apr 1, 2019 at 14:56
0

you could try enums, this will allow you to define a set of named constants. Refer to this document.

enum Constants {
    STUDENT_NAMES= ["JOHN","BOB","NICK"]
    TEACHER_NAME = ["HARRY","CHRIS"]
    SCHOOL_CODE = [100,102,107];
}
-2

Add all your constants in environment.ts file - this is the best practice and there will be no change in the values basd on the environment specific

Add both in environment.ts and environment.prod.ts - this will be your global object and can be accessed at any level

Importing will be same as importing an interface or any class Thanks - Happy coding

2
  • 13
    I would only recommend using environment variables for things that change between dev, test and prod environments like api endpoints, I would not pollute it with every constant in your application. Commented Nov 7, 2018 at 0:58
  • Then you can export a separate const. File and get the data
    – Rahul
    Commented Nov 7, 2018 at 2:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.