atomiks / tippyjs

Tooltip, popover, dropdown, and menu library
https://atomiks.github.io/tippyjs/
MIT License
11.91k stars 518 forks source link

Adjust types to accept `undefined` props #1011

Open macmillen opened 2 years ago

macmillen commented 2 years ago

Problem

TypeScript 4.4 introduced a new compiler option exactOptionalPropertyTypes that fixes some weak typings by differentiating between undefined and "not present" which results in type errors when passing undefined values to tippy's Partial<Props>.

I found this piece of code in src/createTippy.ts which takes care of the undefined props by using removeUndefinedProps onto the passed props:

const props = evaluateProps(reference, {
  ...defaultProps,
  ...getExtendedPassedProps(removeUndefinedProps(passedProps)),
});

Solution

That means that we should probably allow undefined as a prop value by adjusting the type of Props because all undefined prop values get stripped away by removeUndefinedProps anyway. Something like this maybe:

type OptionallyUndefined<T> = { [K in keyof T]: T[K] | undefined };

type PassedProps = Partial<OptionallyUndefined<Props>>;

This is how it looks if the exactOptionalPropertyTypes flag is enabled:

image

atomiks commented 2 years ago

Is this something that affects consumers or is it just an internal issue?

macmillen commented 2 years ago

It affects customers who have this Typescript flag enabled.