S
SolidJS•10mo ago
Silverdagger

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?
No description
11 Replies
Martnart
Martnart•10mo ago
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.
Silverdagger
Silverdagger•10mo ago
I accidentally sent the wrong image While I was attempting various ways to fix it - so I changed it later sorry for that
Martnart
Martnart•10mo ago
Basically, TS doesn't know from a function call that something is defined. It is different when you have something like
const myThing = myAccessor()
myThing // defined
const myThing = myAccessor()
myThing // defined
vs.
if (myAccessor() && myAccessor().someThing) // TS assumes the fn might still be undefined
if (myAccessor() && myAccessor().someThing) // TS assumes the fn might still be undefined
Edit: so you tell it that it's defined:
if (myAccessor() && myAccessor()!.someThing) // TS is okay
if (myAccessor() && myAccessor()!.someThing) // TS is okay
Glad I'm not crazy 😄 I doubted myself there for a second
Tommypop
Tommypop•10mo ago
Martnart
Martnart•10mo ago
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
// non-keyed w/o callback... - only updates the one expression, needs ! assertion
<Show when={user()}>
<div>{user()!.name}</div>
</Show>
// non-keyed w/o callback... - only updates the one expression, needs ! assertion
<Show when={user()}>
<div>{user()!.name}</div>
</Show>
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, sorry
Silverdagger
Silverdagger•10mo ago
THis is the full error text I get .
No description
Martnart
Martnart•10mo ago
What's your routeData look like? It should not return Response
Silverdagger
Silverdagger•10mo ago
export function routeData() {
return createServerData$(async (_, { request }) => {
const user = await getUser(request)
if (user === null) return redirect('/login')
const clans = await prisma.clanmember.findMany({
select: {
clan: { select: { id: true, clanname: true } },
accepted: true,
},
where: { userId: user.id },
})
return {
invitations: clans.filter((invite) => !invite.accepted),
member: clans.filter((member) => member.accepted),
}
})
}
export function routeData() {
return createServerData$(async (_, { request }) => {
const user = await getUser(request)
if (user === null) return redirect('/login')
const clans = await prisma.clanmember.findMany({
select: {
clan: { select: { id: true, clanname: true } },
accepted: true,
},
where: { userId: user.id },
})
return {
invitations: clans.filter((invite) => !invite.accepted),
member: clans.filter((member) => member.accepted),
}
})
}
Martnart
Martnart•10mo ago
Try return redirect('/login') > throw redirect('/login')
Silverdagger
Silverdagger•10mo ago
Alas TS is appeased 😄 thanks. Didn't know you could throw the redirect
Martnart
Martnart•10mo ago
🙂
Want results from more Discord servers?
Add your server
More Posts
Is there some equivalent to <Portal/> that is also compatible with ssr?I want to make a persistent modal dialog, and for that i need the `<Portal/>` component from `solid-Recommended way to track the setter of a signal?Currently, my solution involves wrapping the setter with a function. Playground: https://playground.Is it possible to pass paramters a function wrapped by a createEffect/createMemo?,I have a certain function in my component which I have noticed runs multiple times since I am referIs it possible to implement LiveViewJS type functionality using SolidJS + SSR?Specifically, simple example would be : an Observable (for example from rxjs or S.js ) keeps updatinNeed to pass a function as a dependency to the solid js web component.Hello I have made a web component with solid and I need to pass a function as a dependency to it. I Adding components that use context to the DOM after mountI have a component (ShelfScene) that returns a mapped list of items (Shelf). The ShelfScene componenFetching data with authorisation tokenI’m using auth.js with cognito and my backend requires the authorisation token to be present with evFor loop in route breaks when form in another component is submittedI have this <For> loop in a route. Works fine.... UNTIL..... There is another component I can useinput elements become empty upon hydrationIf I load a page developed using solid start on a slow connection, and enter some text in an input eMissing Http method returns 200 status code on api routeI am trying to generate API routes. In the docs I read that 405 is returned for non existing routes