Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript class which has only the constants

I would like to keep my cache keys in a central place.I would like to do it as a constant file.At this moment I have declared cache keys each and every page where it's required.But I need to remove that duplication.How can I do that?

One of cache key declaration:

purchasedOfflineArticlesKey: string = 'myLibraryPurchasedOfflineArticles';

Can you tell me a proper design for this? Do I need to create a class for this and use that class in other places where it needs or any other way? Hope you'll give feedback for this.

Update:

I have cache service like below.

local-cache-service.ts

import { Injectable } from '@angular/core';
import { CacheService } from "ionic-cache";

@Injectable()
export class LocalCacheServiceProvider {

  constructor(private cache: CacheService) { }

  //get Item
  getItem(key: string): Promise<any> {
    return this.cache.getItem(key);
  }
}

I have used it like this:

offline-articles.ts

private purchasedOfflineArticlesKey: string = 'myLibraryPurchasedOfflineArticles';

 constructor(private localCacheService: LocalCacheServiceProvider) {
  }

     ionViewDidLoad() {
        this.localCacheService.getItem(this.purchasedOfflineArticlesKey).then(values => {
          this.arrangeMyOfflineArticles(values);
        }).catch(reason => {
        });
      }
like image 331
Sampath Avatar asked Aug 03 '17 10:08

Sampath


People also ask

How do I create a constants class in TypeScript?

Use the readonly modifier to declare constants in a class. When a class field is prefixed with the readonly modifier, you can only assign a value to the property inside of the classes' constructor. Assignment to the property outside of the constructor causes an error.

How do I store a constant in TypeScript?

Use Class with static properties One way to manage constants in TypeScript is to use the class. In the following example, the AppSettings class is created with static properties to define constants. Those properties are applied with readonly modifiers, thus they can not be reassigned.

What is a constants class?

A class constant is a field that is declared with the static and final keywords. As a reminder, the final keyword indicates that the field reference cannot be changed to point to a different value.

How do you define a static class in TypeScript?

A static class can be defined as a sealed class that cannot be inherited besides being inherited from an Object. static class cannot be instantiated, which means you cannot create the instance variable from the Static Class reference.


2 Answers

In one of my projects, I have defined a namespace for this purpose in a file named constants.ts. You can do the same. Below is some sample code:

export namespace AppConstants
{
    // Class for general global variables.
    export class General
    {
       public static readonly WELCOME_TITLE = 'Welcome to my App';
    };
}

In my app where I want to use the constants, I am importing this namespace:

import { AppConstants } from './core/common/constants';

I can access these constants like:

myMethod(){
    console.log(AppConstants.General.WELCOME_TITLE);
}
like image 138
Faisal Avatar answered Nov 10 '22 12:11

Faisal


Classes shouldn't be used for something that cannot benefit from class instantiation.

One option is to have them as exports in separate file, this way non-existent keys can be resolved on import:

export const foo = 'foo';
export const bar = 'bar';

Alternatively, cache keys can be declared as object keys, e.g. in the place where cache service is declared.

export const CACHE_KEYS = {
    foo: 'foo',
    bar: 'bar'
};

@Injectable()
export class LocalCacheServiceProvider {
  constructor(private cache: CacheService) { }

  getItem(key: keyof typeof CACHE_KEYS): Promise<any> {
    return this.cache.getItem(key);
  }
}

The service can make use of keyof constraint to limit the keys to known ones.

Depending on the design of the application, LocalCacheServiceProvider and key set can be extended per module/component to provide unique set of keys for the unit.

like image 21
Estus Flask Avatar answered Nov 10 '22 12:11

Estus Flask