In TypeScript,when I use myMap to extends Map,but it‘s show Expected 0 argument

Viewed 63

Can't the Map of js accept a parameter? Is there any expert who can teach me?

class myMap extends Map {
  constructor(props: any) {
    super(props);
  }
}

enter image description here

1 Answers

The type definition of Map is...

interface MapConstructor {
    new(): Map<any, any>;
    new<K, V>(entries?: readonly (readonly [K, V])[] | null): Map<K, V>;
    readonly prototype: Map<any, any>;
}

which gives 2 constructor functions...

  1. Non-Generic new which doesn't have any parameters
  2. Generic new<K, V> which takes 1 parameter

Both of which return(s) an interface of Map<K, V>. Now when we are extending myMap with a non-generic Map, the constructor available is parameter-less. To be able to pass a parameter, we can write the definition of myMap as such, unless we have any other reason not to..

class myMap extends Map<string, string> {
  constructor(props: any) {
    super(props)
  }
}

where Map<string, string> is just an example for illustration. We will have to decide upon the actual type of Map<K, V> from the context of the usecase.

Related