Home > Blockchain >  Typescript NetworkInformation type does not contain all properties
Typescript NetworkInformation type does not contain all properties

Time:10-23

I want to access effectiveType and downlink properties of the enter image description here

What should I do to get past the typescript errors?

Property 'downlink' does not exist on type 'NetworkInformation'.ts(2339)

CodePudding user response:

Problem

The type for NetworkInformation exists in lib.dom.d.ts and, at the time of writing, looks like this:

interface NetworkInformation extends EventTarget {
    readonly type: ConnectionType;
}

lib.dom.d.ts L10430-L10432 @ 65ae16c

Unfortunately downlink and effectiveType (and more) do not exist on there yet.

The DOM lib is actually generated by a tool: TypeScript-DOM-lib-generator. In the readme, it has the following:

Why is my fancy API still not available here?

A feature needs to be supported by two or more major browser engines to be included here, to make sure there is a good consensus among vendors: Gecko (Firefox), Blink (Chrome/Edge), and WebKit (Safari).

reference

When the type is missing

It's possible that the automated algorithm decided that it's not well supported by browsers and thus removed it. Say we want to add a new interface named Foo. Check if there is a document about that interface in MDN. If there is, see the browser compatibility section and check whether it's supported by two or more browser engines. (Note that Chromium-based browsers use the same browser engine and thus support from them counts as a single support.)

reference

Checking the browser compatibility table for NetworkInformation, it looks like this at the time of writing:

Chrome Edge Firefox Internet Explorer Opera Safari
NetworkInformation (Experimental
  • Related