Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot extend Map #1858

Closed
philipp-sorin opened this issue Jan 2, 2019 · 10 comments
Closed

Cannot extend Map #1858

philipp-sorin opened this issue Jan 2, 2019 · 10 comments

Comments

@philipp-sorin
Copy link

Hey,
Mobx behaves incorrectly with classes extended from build-ins like Map<K, V>
For example (TS)

class StringToStringMap extends Map<string, string> {
  public findOne(id: string): Optional<string> {
    /// code
  }
 }

class Store  {
  @observable
  public stringToStringMap: StringToStringMap  = new StringToStringMap(); 
  // actual type is ObservableMap, method findOne is lost

  @action
  public doStuffWithString(someString: string) {
    this.stringToStringMap.findOne(someString); // throws an error TypeError: findOne is not a function
  } 
}
@fi3ework
Copy link
Contributor

fi3ework commented Jan 3, 2019

I test your code and it works fine. My TS version is 3.2.2.

update: I was wrong. I only checked if TS is working fine. However, it goes fine because StringToStringMap is an instance of StringToStringMap. There's still will be an error as you described in runtime.

@philipp-sorin
Copy link
Author

philipp-sorin commented Jan 3, 2019

Sounds strange because test function is written like value instanceof Map which satisfies my case

@mweststrate
Copy link
Member

mweststrate commented Jan 3, 2019 via email

@philipp-sorin
Copy link
Author

@mweststrate
But it's not a direct Map, it's a sub type of the Map.
It should rather throw an Error if it cannot be converted, but silently change subtype to another subtype of base class looks like not good idea.

Because now, instead of extending Map and creating own observable using Atom, I have to write composite for Map. And this leads to the loss of some features, such as instanceof

@mweststrate
Copy link
Member

mweststrate commented Jan 3, 2019 via email

@philipp-sorin
Copy link
Author

@mweststrate
Ok, maybe check it later when I will have time, so the issue seems to be closed by now?
Or should it be a feature request?

@mweststrate
Copy link
Member

mweststrate commented Jan 4, 2019 via email

@mweststrate
Copy link
Member

Subclassing Map will be forbidden in the next version, as inheritance trees will be lost during cloning. Instead, use generic utilities like:

public findOne(map someMap, id: string): Optional<string> {
  // code operation on map
}

@mweststrate
Copy link
Member

Addressed and released in 4.9.0 / 5.9.0

@lock
Copy link

lock bot commented Jul 21, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants