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

Spreading identical values with properties and indexers creates errors #8276

Closed
chrbala opened this issue Jan 31, 2020 · 4 comments
Closed
Labels

Comments

@chrbala
Copy link

chrbala commented Jan 31, 2020

Similar to #8178, but with an additional property. Spreading identical values with properties and indexers creates errors.

export type Trie<T> = {
  [string]: Trie<T>,
  _?: T,
};

const x: Trie<number> = { };
const y: Trie<number> = { };

const z: Trie<number> = { ...x, ...y };

Flow version: 0.117.0

Expected behavior

Two values with identical types should always be spreadable

Actual behavior

Error is thrown:

    11: const z: Trie<number> = { ...x, ...y };
                                           ^ Cannot determine a type for object literal [1]. `Trie` [2] cannot be spread because the indexer string [3] may overwrite properties with explicit keys in a way that Flow cannot track. Can you spread `Trie` [2] first or remove the indexer?
        References:
        11: const z: Trie<number> = { ...x, ...y };
                                    ^ [1]
        9: const y: Trie<number> = { };
                    ^ [2]
        4:   [string]: Trie<T>,
              ^ [3]
  • Link to Try-Flow or Github repo: tryflow
@chrbala
Copy link
Author

chrbala commented Jan 31, 2020

Interestingly, this works if passed into a function, despite having the same effect.

/* @flow */

export type Trie<T> = {
  [string]: Trie<T>,
  _?: T,
};

const x: Trie<number> = { };
const y: Trie<number> = { };

const merge = <T>(a: T, b: T) => ({ ...a, ...b });
const z: Trie<number> = merge(x, y);

Test on tryflow.

@jbrown215
Copy link
Contributor

That's an unsoundness in generic checking.

I have work in the pipeline that allows this, but it exposes some nastier issue somewhere else in the type checker that makes it unshippable. Specifically, it creates an error in a library definition with no references to actual source code.

@geraintwhite
Copy link

geraintwhite commented Oct 21, 2020

A similar bug makes it impossible to spread on indexed object into a literal object matching the same type.

tryflow 1

tryflow 2

chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this issue Jan 21, 2021
These obviously didn't get fixed in the RN v0.63 upgrade, as we'd
thought they would...

It turns out that it's a very similar bug [1] to the one we
originally thought it was [2], but slightly different.
`UserPresence` is an object type with an indexer *and* an additional
property.

[1] facebook/flow#8276
[2] facebook/flow#8178
chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this issue Jan 21, 2021
These obviously didn't get fixed in the RN v0.63 upgrade, as we'd
thought they would...

It turns out that it's a very similar bug [1] to the one we
originally thought it was [2], but slightly different.
`UserPresence` is an object type with an indexer *and* an additional
property.

[1] facebook/flow#8276
[2] facebook/flow#8178
gnprice pushed a commit to chrisbobbe/zulip-mobile that referenced this issue Jan 22, 2021
These obviously didn't get fixed in the RN v0.63 upgrade, as we'd
thought they would...

It turns out that it's a very similar bug [1] to the one we
originally thought it was [2], but slightly different.
`UserPresence` is an object type with an indexer *and* an additional
property.

[1] facebook/flow#8276
[2] facebook/flow#8178
Gautam-Arora24 pushed a commit to Gautam-Arora24/zulip-mobile that referenced this issue Feb 2, 2021
These obviously didn't get fixed in the RN v0.63 upgrade, as we'd
thought they would...

It turns out that it's a very similar bug [1] to the one we
originally thought it was [2], but slightly different.
`UserPresence` is an object type with an indexer *and* an additional
property.

[1] facebook/flow#8276
[2] facebook/flow#8178
chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this issue Mar 10, 2021
Normally, we wouldn't upgrade Flow except in the same commit as we
upgrade React Native. That's because the code for a React Native
release (the code in node_modules/react-native) is only guaranteed
to pass type-checking with the Flow version that was used to check
the code at that release.

Given that this commit breaks from our normal practice, it's
unsurprising to see a Flow error in a file in
node_modules/react-native. Suppress type-checking in that specific
file (it's just one file), noting that it's not a file that we use
directly, nor does it seem likely that the suppression propagates to
a type-checking lack in our own project. (The most relevant-looking
site I've seen, our use of `UIManager` in src/ZulipMobile.js, still
has its types after this change.)

The nice thing we get in Flow v0.125 is that `$FlowIssue` and
`$FlowExpectedError` are added as default suppression comments [1],
which will allow us to remove the `suppress_comment` lines in our
Flow config that define those. And that's desirable because
`suppress_comment` has been removed from Flow in v0.127 [2], and
we'll have to take that removal along with the Flow version we take
with RN v0.64.

Best, then, to handle as much of this change as we can before the
main RN v0.64 upgrade commit.

In our own code, we have to slightly relocate a suppression comment
for facebook/flow#8276; so, do that.

[1] https://github.com/facebook/flow/blob/master/Changelog.md#01250
[2] https://flow.org/en/docs/config/options/#toc-suppress-comment-regex
gnprice pushed a commit to chrisbobbe/zulip-mobile that referenced this issue Mar 10, 2021
Normally, we wouldn't upgrade Flow except in the same commit as we
upgrade React Native. That's because the code for a React Native
release (the code in node_modules/react-native) is only guaranteed
to pass type-checking with the Flow version that was used to check
the code at that release.

Given that this commit breaks from our normal practice, it's
unsurprising to see a Flow error in a file in
node_modules/react-native. Suppress type-checking in that specific
file (it's just one file), noting that it's not a file that we use
directly, nor does it seem likely that the suppression propagates to
a type-checking lack in our own project. (The most relevant-looking
site I've seen, our use of `UIManager` in src/ZulipMobile.js, still
has its types after this change.)

The nice thing we get in Flow v0.125 is that `$FlowIssue` and
`$FlowExpectedError` are added as default suppression comments [1],
which will allow us to remove the `suppress_comment` lines in our
Flow config that define those. And that's desirable because
`suppress_comment` has been removed from Flow in v0.127 [2], and
we'll have to take that removal along with the Flow version we take
with RN v0.64.

Best, then, to handle as much of this change as we can before the
main RN v0.64 upgrade commit.

In our own code, we have to slightly relocate a suppression comment
for facebook/flow#8276; so, do that.

[1] https://github.com/facebook/flow/blob/master/Changelog.md#01250
[2] https://flow.org/en/docs/config/options/#toc-suppress-comment-regex
@SamChou19815
Copy link
Contributor

This is expected behavior that we don't change

@SamChou19815 SamChou19815 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants