facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.08k stars 1.85k forks source link

[feature request] Covariant function arguments #5392

Closed Andarist closed 1 month ago

Andarist commented 6 years ago

It would be super nice to mark any function arguments as covariant, without that we need to make ugly hacks like this

bradennapier commented 6 years ago

You can set the generics that you assign to the arguments as covariant, but don't think that is what you are wanting here.

Honestly don't really understand what it does specifically at this point. I do know that it is critical to make some things work at times though. I believe it has to do with changing the "type" when using unions but not much docs on it.

Example

Andarist commented 6 years ago

Nope, it's unfortunately not the same - look here

Also would love some docs about setting a variance on generics, no idea what it does at the moment ¯\(ツ)

jesseschalken commented 6 years ago

Try https://flow.org/en/docs/config/options/#toc-experimental-const-params-boolean

jesseschalken commented 6 years ago

Also, I don't see why extracting a variable causes the code to pass. Even if the new variable is declared with let or var, Flow still seems to narrow it in the else of the ternary because there are no assignments to it anywhere (it's "effectively const"). Why doesn't the same apply to the parameter?

Andarist commented 6 years ago

Hm, I was pretty sure that changing the declaration to let or var would cause the same thing as having it in the argument. It seems though that flow is preserver the refinement in that case too.

There is a possibility that flow treats arguments as reassignable object (which it is in non strict mode) and fears to make the assumption that it won't change. That would be quite weird behaviour of flow, shouldn't assume such things.

The reason might ofc me completely different, now knowing that let and vars are keeping their refinements I would categorize my issue as a bug.

EDIT:// also thanks for sharing the experimental.const_params. I was not aware that this is an option and it has fixed my issue

EDIT2:// I have module.use_strict=true in my config, so in no way it should consider arguments reassignment a likely cause to bail out from refined values