how to define static constants in typescript and angular

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;

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)