Typesafe way to render conditionally based on whether array has zero, one or many elements?
Given an array with one or more elements, the idea is to display different UIs. With React, this can be done in a typesafe way by returning different JSX after an
if
check,
How can the same be achieved with Solid? Seems that <Switch /> / <Match />
was a good place to start, although the where
's boolean check doesn't perform type narrowing, so the typecheck fails: the array could be empty and accessing the first element could return undefined
.
When adding <Show />
to the mix, the type safety can be improved,
although this seems overly verbose, and I feel uneasy about using <Show>
just for its non-null assertion. Are there better options?2 Replies
if the array is static, just go with react version
if it's not, this might be better:
it should work the same way as
array().length > 1
will still be memoized
but it may not be worth the "typesafety"Much appreciated @thetarnav . After trying out your suggestion and a few others, I landed on the following,
it ain't pretty, although for now I feel it does a decent job. It captures in one place, and in close proximity to the
Match
, all related type assertions. The inline function makes it clear that something special is going on, and avoids having to create a new component and pass down all the props. Using <Show>
made it difficult to understand that it was being used for type narrowing rather than business logic.
Just came back to this and possibly this may be the best option yet: constructing a final value in the where
's expression that gets passed as a non-null asserted value to the child render function,