Search code examples
htmltypescriptdommediaw3c

Access of properties of HTMLMediaElement Interface in some another package


In a package A, there is an interface named HTMLMediaElement which had some properties i.e.,

HAVE_METADATA: number;
HAVE_CURRENT_DATA: number;
HAVE_NOTHING: number;
NETWORK_NO_SOURCE: number;
HAVE_ENOUGH_DATA: number;
NETWORK_EMPTY: number;
NETWORK_LOADING: number;
NETWORK_IDLE: number;
HAVE_FUTURE_DATA: number;

The HTMLMediaElement interface is implemented by Video class, which returns the value of these properties as:

get HAVE_CURRENT_DATA(): number {
        return 4;
}

HTMLMediaElement interface and Video class both are exported.

https://html.spec.whatwg.org/multipage/media.html#dom-media-have_current_data:~:text=4.8.11.7-,Ready%20states,-media.readyState

HTMLMediaElement interface reference: https://github.com/TypeStrong/tscs/blob/master/lib/node_modules/TypeScript/bin/lib.dom.d.ts#L8840

What I want is,

I am having another package B (where I am importing HTMLMediaElement interface and Video class), and want to access the HTMLMediaElement properties like,

HTMLMediaElement.HAVE_CURRENT_DATA as globally in package B.

I don't want to access these properties (HAVE_CURRENT_DATA, HAVE_NOTHING, etc.) by Video class object.


Solution

  • Convert Interface HTMLMediaElement to abstract class and add abstract keyword to properties and methods (so it will work like interface). Add static keyword to readonly Properties:

    static readonly HAVE_CURRENT_DATA : number = 0;
    

    Note: not sure but it works for me, you don't need to change the subclass defintion from “implements” to “extend” class.