Error in a method exported from a module with Typescript

Asked

Viewed 43 times

0

I am trying to export a specific class of a module, so far so it is all right. I declared my class as Abstract not to need initialize it with the new, but when trying to use the function "getaway()" the compiler returns me the following error:

TS2339: Property 'getItem' does not exist on type 'typeof Webstorage'

MODULUS:

interface IStorage {
    setItem(key : string, value: any) : void;
    getItem(key : string) : any;
    getItem(key : string) : any;
}

export abstract class WebLocalStorage implements IStorage {
    public setItem(key : string, value : any) { 
        localStorage.setItem(key, value);
    }

    public getItem(key : string) : any { 
        localStorage.getItem(key);
    }
}

export abstract class WebStorage implements IStorage {
    public setItem(key : string, value : any) { 
        sessionStorage.setItem(key, value);
    }

    public getItem(key : string) : any { 
        sessionStorage.getItem(key);
    }

}

PLACE WHERE THE MODULE IS BEING IMPORTED:

import { WebStorage } from '../../HTMLAPIS/WebStorageAPI';

export default class FilterData {
    private readonly _typingValue: HTMLInputElement;
    private readonly _containerMain: HTMLElement;
    private readonly _listboxMain: HTMLElement;
    private readonly _listboxSecondary: HTMLElement;

    constructor(public typingValue: HTMLInputElement, public containerMain : HTMLElement, public listboxMain : HTMLElement,  public listboxSecondary : HTMLElement) {
        this._typingValue      = typingValue;
        this._containerMain    = containerMain;
        this._listboxMain      = listboxMain;
        this._listboxSecondary = listboxSecondary;
    }

    public initComponent() {
        this.keypress();
    }

    private keypress() {
        this._typingValue.addEventListener('keypress', (e) => {
            console.log(WebStorage.getItem('shopkeepers_names'));
        });
    }

    searchData() {

    }
}

1 answer

0

But abstract doesn’t mean you don’t need to use new, Abstract means that this class cannot be used directly and must be implemented by another class.

To use a method without creating an object, what you need is static.

class WebStorage implements IStorage {
    public static setItem(key : string, value : any) { 
        sessionStorage.setItem(key, value);
    }

    public static getItem(key : string) : string { 
        return sessionStorage.getItem(key);
    }
}

In note: sessionStorage only stores strings. You can even pass a numeric, but this numeric will be converted before it is stored.

  • The problem with this is that the compiler will complain on behalf of the INTERFACE!

  • Oh yes. But if your interface says that the methods cannot be static, then there is no way you can use them without creating an object. You can change the method declaration in the interface, or create different methods in the classes that implement it. If you could contradict the rule you created, which would serve the rule?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.