Narrow types when an object store is declared as a union #291
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This makes some changes to the type declarations so that the value type of an object store can be narrowed based on the key type (and vice versa).
There are some caveats. There's a limitation in the way that TypeScript unions work (see microsoft/TypeScript#18758). It means that the type changes made in this PR cannot be applied to indexes. It also means that narrowing the key type based on a value type when the value type is an object type isn't possible (which affects the typing of
add
andput
).Also, cursor types haven't been updated. Creating a cursor over one value (for example
'foo'
) and expecting the value type to be narrowed (string
instead ofstring | number
) seems like an obscure use-case. It didn't seem worth the added complexity but I'll implement it if requested. A more common use case would be iterating over multiple values for a single key in an index but we can't do that.Resolves #275.