Search code examples
javascripthtmltypescriptlocal-storagesession-storage

How to determine Storage type? Session- or LocalStorage?


I have a variable of type Storage - this is what variable.constructor.name says. How can I now determine is it localStorage or sessionStorage? I just need the name.

localStorage / sessionsStorage.constructor.name

Example code:

class WebStorage {
  constructor(public storage: Storage) {}

  public getStorageName(): 'LocalStorage' | 'SessionStorage' {
     // how to return proper name?
  }
}

Solution

  • I believe you can simply compare it:

    return storage === window.localStorage ? 'LocalStorage' : 'SessionStorage';