Skip to content

[TS] Narrow types for Show utility - #701

Merged
JoviDeCroock merged 4 commits into
preactjs:mainfrom
calebeby:patch-1
Jun 27, 2025
Merged

[TS] Narrow types for Show utility#701
JoviDeCroock merged 4 commits into
preactjs:mainfrom
calebeby:patch-1

Conversation

@calebeby

@calebeby calebeby commented Jun 26, 2025

Copy link
Copy Markdown
Contributor

Here is part of my code:

<Show when={xScale}>
  {(xScale) => xScale.map(...)}
</Show>

xScale has the type ReadonlySignal<number[] | null>. The parameter xScale has the type number[] | null, but really its type should be number[], since the function won't get called for falsy values.

Within the Show component function, the xScale parameter is known to be truthy, since Show won't run the child unless the signal value is truthy.

Before this PR, I get the TS error 'xScale' is possibly 'null'. from right before the .map.

After this PR, there are no errors, since the xScale parameter excludes falsy types from the original type, so its type is just number[] | null.

I do not use React but if you would like me to make the same change to the react utils package I would be happy to.

Thanks for signals! I have used preact for a long time but this is my first time using signals and it is so neat! I get a smile on my face every time a render is skipped because signals 😄

@changeset-bot

changeset-bot Bot commented Jun 26, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a1e13bf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@preact/signals Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify

netlify Bot commented Jun 26, 2025

Copy link
Copy Markdown

Deploy Preview for preact-signals-demo ready!

Name Link
🔨 Latest commit a1e13bf
🔍 Latest deploy log https://app.netlify.com/projects/preact-signals-demo/deploys/685e3e86aafe7400080003cd
😎 Deploy Preview https://deploy-preview-701--preact-signals-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

I don't know if I like that. 0n feels more object-y than 0 to me
Comment thread packages/preact/utils/src/index.ts Outdated
calebeby and others added 2 commits June 25, 2025 18:18
Downside: It won't correctly handle literal values like "hello" vs "", but oh well.

@JoviDeCroock JoviDeCroock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This change can also be done in react if you like.

@JoviDeCroock
JoviDeCroock merged commit 01f406c into preactjs:main Jun 27, 2025
6 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants