Any pattern to avoid getting ts angry when using route data fields?
I am using routeData returning an object with 2 fields from it. When I use a combination of <Show> and <For> tags to display my data and try to access any fields TS complains even though the code works. Atm I can mark the "clans" const as ':any' to avoid this but I feel that is not right. Is there a correct pattern I could follow to avoid this?
11 Replies
I first wrote something about
.latest
I should go to bed since there's not a single .latest
in your code. o.O
Anyway, this is quite common with Solid. The easy answer is: !
This is fine in that it is a known 'problem' and you can safely avoid it.
<Show>
also has a callback pattern, where your returned value is NotNullable
but in this case, since you return a boolean from the when
condition, it wouldn't really make sense.I accidentally sent the wrong image While I was attempting various ways to fix it - so I changed it later sorry for that
Basically, TS doesn't know from a function call that something is defined. It is different when you have something like
vs.
Edit: so you tell it that it's defined:
Glad I'm not crazy 😄 I doubted myself there for a second
You can do something like this: https://discord.com/channels/722131463138705510/722131463889223772/1138900596523536384
It's still not ideal lol
Yes but it's another way of making TS think something. You are fine in this case to do a non-null-assertion. It is kind of encouraged to a certain degree since the latest
<Show>
update. It's just a bit hard to get the typing right. But whatever is guarded by a show, will be defined, basically.
https://github.com/solidjs/solid/releases/tag/v1.7.0
Specifically
You could rewrite your when
to actually return the output such as when={clans()?.invitations.length > 0 && clans()}
and then work with the callback but it seems a bit cumbersome.
Wait I just realize that your typing does seem to be off. That error shouldn't appear after a ?
operator.
https://playground.solidjs.com/anonymous/b542915b-456e-4324-96dc-3106a9cc2f87
Edit: pasted wrong link, sorryTHis is the full error text I get .
What's your routeData look like?
It should not return
Response
Try
return redirect('/login')
> throw redirect('/login')
Alas TS is appeased 😄 thanks. Didn't know you could throw the redirect
🙂