14795

Flowtype: How to use compute method in Class

Question:

I wanna visit the class property according the params as the following code:

class Commander { constructor() {} create() {} test() {} undo() {} redo() {} execute(...args): void { const command: string = args.slice(0); const rest: any[] = args.slice(1); this[command].apply(this, rest); } }

But I got an error as below:

<blockquote>

Cannot get this[command] because an indexer property is missing in Commander <a href="https://flow.org/try/#0MYGwhgzhAEDCD2BbRYB2ATApgJ2gbwChpph5UIAXbAV2AvmwAoBKfAXyJO0zAsxfac+lAXg7FqGeKPHRu6aazGdMAD0zBqfRgDo9YbAHMIzAFzQAbvACW6fJwCQpchRJIUGc5WzXUh6AC80AbGOhAg1sD8AAzMANycxM6UcpiU5mgAngDaALqBwUYQYRFRjACM8YnQFAAW1hDZpMho6Lk6YAAOnSCZjHUNADSplFXEHGxAA" rel="nofollow">1</a>.

</blockquote>

More info you can see the <a href="https://flow.org/try/#0MYGwhgzhAEDCD2BbRYB2ATApgJ2gbwChpph5UIAXbAV2AvmwAoBKfAXyJO0zAsxfac+lAXg7FqGeKPHRu6aazGdMAD0zBqfRgDo9YbAHMIzAFzQAbvACW6fJwCQpchRJIUGc5WzXUh6AC80AbGOhAg1sD8AAzMANycxM6UcpiU5mgAngDaALqBwUYQYRFRjACM8YnQFAAW1hDZpMho6Lk6YAAOnSCZjHUNADSplFXEHGxAA" rel="nofollow">flowtype try</a>.

If I make some stupid things, and please let me know!

Thanks!!

Answer1:

<blockquote>

Thanks your answer, but I wanna the code keep tidy, and I found the another solve <a href="https://flow.org/try/#0MYGwhgzhAEDCD2BbRYB2ATApgJ2gbwChpoASAa0wE8AuaCAF2wEtUBzAbiNIDcwQBXTLTSUuweKgbZ+wevGwAKAJT4AvmOyYw9TMrVcdDPXnXF+GeMdPRN6SypNdMAD0zB+OhQDofYbKwglWm54JnR8LgBIcUl6aHFkNHRaKRZWaABeaD8ArwgQJmBdAAYlTmJiGIYbTAZhVEoAbQBdTOz-CDyCooUARjKuYnoACyYIRoSUDGavMAAHOZBKBRGxgBoahgHidXUgA" rel="nofollow">Try</a>. But use the any keyword, make the check no sense about the static check. – Cin

</blockquote>

If you use any keyword that is the same as just turning flow off for anything that touches that variable.

"If you want a way to opt-out of using the type checker, any is the way to do it. Using any is completely unsafe, and should be avoided whenever possible." from <a href="https://flow.org/en/docs/types/any/" rel="nofollow">Any</a>

This is a safer way: (<a href="https://flow.org/try/#0MYGwhgzhAEDCD2BbRYB2ATApgJ2gbwChpoASAa0wE8AuaAImHlQgBdsBXYF+bO6AH3rBsmMC0x9Bdca0n12GeHLoj0SgNxFSANzAh2mWgAoAlNAC8APmiIAlgA9M6LcNHjT+AL5aZLD3m9iBTV-QOhVeFCtTEdgdndGZDR0ABpoADpMkVYTWm14W3R8LWIWAAtbCABtRJQMAF10sAAHZpBKI3LKtOyWE01ib08gA" rel="nofollow">Try</a>)

class Commander { $key: "create" | "test" | "undo" | "redo"; $value: () => mixed constructor() {} create() {} test() {} undo() {} redo() {} execute(command, ...rest): void { this[command].apply(this, rest); } }

Answer2:

Because the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#Property_names" rel="nofollow">property accessor</a> you're using could be any string, Flow thinks you're trying to use your <a href="https://flow.org/en/docs/types/objects/#toc-objects-as-maps" rel="nofollow">class instance as a map</a>. To get around this, you need to do some sort of checking of the command variable before calling the instance methods. Here's my suggested way how to do it:

(<a href="https://flow.org/try/#0MYGwhgzhAEDCD2BbRYB2ATApgJ2gbwChpph5UIAXbAV2AvmwAoBKfAXyJO0zAsxfac+lAXg7FqGeKPHRu6aazGdMAD0zBqfRgDo9YbAHMIzAFzQAbvACW6fJwCQpchRJIUGc5WzXUh6AC80AbGOhAg1sD8AAzMANycxM6UcpiU5mgAngDaALqBwUYQYRFRjACM8YnQEADu1hTAABaMpMho6ErVSZCY0ABEwhT9pt3E0BRN1sVDOmAADvMgmYyT0wA0qZRV47vQAEbcYADWCXskvQOSCiNjxGvF1-Bzi8urUxCb3Ntn5wdHpzGwEu-Xk8FufwmHx0YJeSxWDy+aQoOz+hx4gPOWAAZmBqCAKKNIZNsPBatBUJhyQBRbCkpj9ACSqAsYAidjaHnQ-VRxFkHDYQA" rel="nofollow">Try</a>)

class Commander { constructor() {} create() {} test() {} undo() {} redo() {} execute(...args): void { const command: string = args.slice(0); const rest: any[] = args.slice(1); switch(command) { case "test": this.test.apply(this, rest); break; case "undo": this.undo.apply(this, rest); break; case "redo": this.redo.apply(this, rest); break; default: throw new Error("Invalid command"); } } }

You could also replace the method.apply(this, rest) syntax with method(...rest) to keep things a little cleaner, but that's stylistic ands not very relevant to your question.

Recommend

  • Golang Build in docker not finding local import
  • ManagementObjectSearcher select network printers?
  • CDI: Injecting single instance works, but injecting Instance does not. Why?
  • cannot be assigned to — it is read only - C#
  • Typecasting `this` of a base class template to its derived class
  • How to initialize context? [closed]
  • How to display a tooltip according to mouse position? - JavaFX
  • Watson Conversation - Why is the ANYTHING ELSE node not chosen
  • Merge Module leaving files during uninstall
  • Connecting bluetooth device to windows phone 8 application
  • Compress a file with GZipStream while maintaining its meta-data
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • Android device acting as an accessory
  • Implicit joins and Where in Doctrine - how?
  • I18n locale disregarding fallbacks
  • converting text file into xml using php?
  • revitapi ironpython ToRoom returns “indexer # object”
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • jQuery ready not fired after rails link_to is clicked
  • htaccess add www if not subdomain, if subdomain remove www
  • MongoDb aggregation
  • JSON response opens as a file, but I can't access it with JavaScript
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Bad request using file_get_contents for PUT request in PHP
  • How to rebase a series of branches?
  • Accessing IRQ description array within a module and displaying action names
  • Jquery - Jquery Wysiwyg return html as a string
  • Arrays break string types in Julia
  • Matrix multiplication with MKL
  • WPF Applying a trigger on binding failure
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • File not found error Google Drive API
  • Linking SubReports Without LinkChild/LinkMaster
  • Is it possible to post an object from jquery to bottle.py?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass