0

Why do we need json.parse and json.stringify while dealing with localStorage object? What if we do not use JSON.parse and JSON.stringify(value) in below code?

In Get method why can not we say localStorage.getItem(key) as T;

export class StorageService 
{
    constructor() { }

    public Get<T>(key: string): T
    {
        return JSON.parse(localStorage.getItem(key)) as T;
    }

    public Set(key: string, value: any): void
    {
        localStorage.setItem(key, JSON.stringify(value));
    }

    public Clear()
    {
        localStorage.clear();
    }

    public Remove(key: string)
    {
        localStorage.removeItem(key);
    }
}
Ozmen
  • 129
  • 3
  • 10

0 Answers0