Saturday, 26 June 2021

Overriding Object.entries()'s types causes an unexpected error

I overrode Object.entries like this:

interface ObjectConstructor {
  entries<T extends Record<string, any>>(o: T): {
      [K in keyof T]: [K, T[K]];
  }[keyof T][];
}

It usually works fine. However, in some cases, I'm getting:

Type '["alignContent", AlignContent | undefined] | ["alignItems", AlignItems | undefined] | ["alignSelf", AlignSelf | undefined] | ... 781 more ... | undefined' must have a '[Symbol.iterator]()' method that returns an iterator.

The last type in the union is undefined, making it unable for me to destructure like this:

const style: Partial<React.CSSProperties> = {
  height: '123px',
};
for (const [k, v] of Object.entries(style)) {
  ...
}

According to the type definition, it should never return undefined. Why is it returning undefined? I guess it's safe to just omit undefined.



from Overriding Object.entries()'s types causes an unexpected error

No comments:

Post a Comment