epicweb-dev / client-hints

Eliminate a flash of incorrect content by using client hints
https://github.com/epicweb-dev/client-hints
257 stars 10 forks source link

FIX: Custom Hint Type (non-string) fallback issue #11

Open zemaphore opened 2 months ago

zemaphore commented 2 months ago

Hey Kent,

I noticed that those // @ts-expect-error lines were hiding a type error for the call of the transform function if no cookieString value is present.

Here:

acc[hintName] = hint.transform(getCookieValue(cookieString, hintName) ?? hint.fallback)

,the fallback value would be passed to hint.transform. But by type definition it is already of target type and does not need any transformation. I added a test using a "complex" type to demonstrate.

I fixed the issue and also simplified the types to pass the TypeScript scrutiny. As this made the ClientHintsValue type obsolete, I took the liberty to move the ClientHint type to the main file and get rid of the utils one.

Let me know if you agree with the changes or need further changes.

Best Alex

kentcdodds commented 1 month ago

Thanks! These changes break types in the Epic Stack because the Record<string, unknown> bit which results in the hints type resolving to unknown. Can you look into that?