-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
TypeScript doesn't understand fromEntries
#43332
Comments
We don't have any type constructors that could do this, so the definition is written as well as it can be. |
If you refactor the |
It is a |
The type system is powerful enough to do better than this. type UnionToIntersection<Union>
= (
Union extends unknown
? (distributedUnion: Union) => void
: never
) extends ((mergedIntersection: infer Intersection) => void)
? Intersection
: never
type Entry<T, K = keyof T>
= K extends keyof T
? [K, T[K]]
: never
type _FromEntry<T extends readonly [PropertyKey, unknown]>
= T extends unknown
? { [k in T[0]]: T[1] }
: never
type FromEntry<T extends readonly [PropertyKey, unknown]> = UnionToIntersection<_FromEntry<T>>
interface ObjectConstructor {
entries<T>(o: T): Entry<T>[]
fromEntries<T extends readonly [PropertyKey, unknown]>(entries: Iterable<T>): FromEntry<T>
keys<T>(o: T): (keyof T)[]
} Some type definitions at first glance seem unnecessary: they're written that way to take advantage of distributive conditional types. 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:
|
Look at this example
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
The text was updated successfully, but these errors were encountered: