profile
viewpoint

Ask questionsGeneric type not scoped as expected in closure.

<!-- Please fill out this entire template so that we can address your bug report as quickly as possible.

Any bug reports that don't contain instructions to reproduce the issue will be closed. -->

Flow version: v0.129.0

Expected behavior

The flow documentation about generics makes this claim:

Generic types work a lot like variables or function parameters except that they are used for types. You can use them whenever they are in scope.

In this example, I'd expect the generic "T" type to be to be a string everywhere it is in scope.


function entries<T>() {
  let entries: T[] = []

  return {
    push: (entry: T) => {
      entries.push(entry)
    }
  }
}

let e = entries<string>()

e.push("abc") // <---- ERROR HERE

Actual behavior

This error is given.

    14: e.push("abc")
               ^ Cannot call `e.push` with `"abc"` bound to `entry` because string [1] is incompatible with `T` [2]. [incompatible-call]
        References:
        14: e.push("abc")
                   ^ [1]
        6:      push: (entry: T) => {
                           ^ [2]

<!-- Please reproduce your issue on flow.org/try so that we can debug it.

Not all issues are reproducible on try-flow because they may require multiple files or specific flowconfig settings. If your bug can only be reproduced under one of these constraints, please make a small github repo that contains a minimal way to reproduce your problem. -->

  • Link to Try-Flow or Github repo: https://flow.org/try/#0FAMwrgdgxgLglgewgAgKYRgJzqgzgHgBUA+ACgEpkBvYASABtUY0Ns8AuZQgbQF1kAvMj7A6mJmEwoatWgAcwuABadS6LAE9OhSgOLU6tZMfVtcAOgXK1rDeUMBfOk6fBGzVIJZYcBXD4gAczJ7YFRLRSVSACIAQwAjKGjyIA
facebook/flow

Answer questions dsainati1

This is not exactly a bug, but neither is it the desired behavior. @mvitousek is almost done with a set of changes that should improve the behavior here to be more in line with what you expect. So a "fix" is in the works.

I closed the issue because there is a work-around in the mean time.

useful!

Related questions

Common spread pattern leads to "exponentially large number of cases" errors hot 1
Flow issue "Please upgrade flow to version >=0.75." hot 1
Object.fromEntries type defs missing hot 1
Cannot set defaultProps with React.memo and React.forwardRef hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
Inexact rest object claims it may have properties it can't possibly have hot 1
0.92.0 release crashes on startup (Unix.Unix_error) hot 1
module.name_mapper does not find nested modules on windows hot 1
Cannot import useState because there is no useState export in react. hot 1
Can't use a default value from a destructuring assignment as a computed property since 0.111 hot 1
Support optional chaining - flow hot 1
Flow issue "Please upgrade flow to version >=0.75." hot 1
Allow explicitly including a folder under an ignored path hot 1
Flow caches and fails to purge errors hot 1
source:https://uonfu.com/
Github User Rank List