tippyjs icon indicating copy to clipboard operation
tippyjs copied to clipboard

Adjust types to accept `undefined` props

Open macmillen opened this issue 3 years ago • 2 comments

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

macmillen avatar Dec 03 '21 12:12 macmillen

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

atomiks avatar Dec 09 '21 12:12 atomiks

It affects customers who have this Typescript flag enabled.

macmillen avatar Dec 10 '21 14:12 macmillen