You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an initial object exchanges (key: name of the exchange, value: Object).
Then I declare a type to have all the exchange names in a string literal type.
I declare another object representing a wallet for each exchange in a separator data structure.
Finally I create wallets object based on this last structure. I expect no error since I am only creating a structure similar in keys of the first one only a different value type that map take care to create.
But wallets variable is having an error saying :
Type '{ [k: string]: []; }' is missing the following properties from type 'WalletsData': kraken, binance, others
If you refactor the Object.fromEntries(Object.keys(..... code into a helper function that takes a value selector predicate, you can usually put a type on the helper function that lets you have strong typing when calling that function using mapped types. The types for lodash may have some inspiration in them.
Some type definitions at first glance seem unnecessary: they're written that way to take advantage of distributive conditional types.
Distributivity in Entry produces a union of ordered-pairs instead of a single ordered-pair of unions. UnionToIntersection takes advantage of covariance in function parameters & distributivity to infer an intersection. FromEntry distributively maps each ordered-pair of a union to a singleton key/value mapping, then intersects that union to merge all those mappings.
With these definitions, your code (lightly modified to explicitly type ordered pairs) passes type check.
I believe this is a duplicate of #35745. As mentioned there:
It's not correct to use the input type to determine the output type, because knowing what might be in an array is not the same as knowing what's actually in it. This program is legal per the above definitions but unsound:
Activity
RyanCavanaugh commentedon Mar 22, 2021
We don't have any type constructors that could do this, so the definition is written as well as it can be.
nmain commentedon Mar 23, 2021
If you refactor the
Object.fromEntries(Object.keys(.....code into a helper function that takes a value selector predicate, you can usually put a type on the helper function that lets you have strong typing when calling that function using mapped types. The types forlodashmay have some inspiration in them.yw662 commentedon Jul 7, 2021
It is a
Object.fromEntriesissue but also aObject.keysissue before that.You may right your own version for
Object.fromEntriesto workaround that issue.like this:
https://www.typescriptlang.org/play?target=99&jsx=0#code/MYewdgzgLgBApmKAnAlnCMC8AoGeYA8AKgHwAUcAXDEQJRYm754DyARgFZzBQB0CyNBAr0AhhgDeTZngDaAaRgowMANZwAniABmNALrUFAGhoK9e6fgC+s9Vt1E9si9lCRY2pCAC2AUUSo6FgwTASKcAAeUAgAJhjQqGAA5iYAauQCgRCG8kapTnr0mCQhzOxcPLyePv6C6BQBQmKSlnJ2SiryBjCpllbYruDQ8BHAABaiyUGYMFJ4qkii6mDUYHAA7jDl3FBGTGzKk8BUMGub2zx7eCBQY3BI2acbW5w72P3YMdwANqJIcDAoBoAA4AgCCADdRChfmxvnBfKMJlMMDM7DpASC4BjIuNJkl0ABuAZfYC-f6Y0EwADqom+8KgEAAIqIoKJgnMYLZNB0YJDobD4Yi8Sjus53gNBu4YEikrUsoZ0bogaCcUj8egTM4CsFMkIKOqUbReN5RMCyGRbCZCsUrVzzLQBm5hus6QzHrT6XBGSy2cFqn5GvVZfKmkA
lmmarsano commentedon Mar 21, 2022
The type system is powerful enough to do better than this.
I often override the built-in type definitions if it's bad enough.
Some type definitions at first glance seem unnecessary: they're written that way to take advantage of distributive conditional types.
Distributivity in
Entryproduces a union of ordered-pairs instead of a single ordered-pair of unions.UnionToIntersectiontakes advantage of covariance in function parameters & distributivity to infer an intersection.FromEntrydistributively maps each ordered-pair of a union to a singleton key/value mapping, then intersects that union to merge all those mappings.With these definitions, your code (lightly modified to explicitly type ordered pairs) passes type check.
mxdvl commentedon Jul 6, 2022
I believe this is a duplicate of #35745. As mentioned there: