Typesafe <Show> only when store has data
the end result I want
I've tried various things like
createStore<StoreType | {}>({});
-> results in ts errors X doesn't exist on {}
re assigning store to previous declarations
the easy way around this is to create store type object of just empty values until the real values come, but that just seems lame :
28 Replies
Try
(I used
'date'
, but any property should work)its never coming past <show now for me>
I also tried this put it always passed <show
{mockDataStore => {
just gives me syntax errors
I also tried this signal but it always passed
<Show when={dataLoaded()}>
const [dataLoaded, setDataLoaded] = createSignal(false);
and later
setDataLoaded(true)
I don't know what to tell you because this is syntactically valid 🤷
i bit the bullet and did this
we do that all over our apps atm
okay 🤷
its not a great ad for solid
but still prefer 100x to react
It works fine for me, don't know what to tell you
got an example?
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
I didn't show it in this example, but inside the callback you can safely access any properties like so
mockDataStore().address.addressLine1
ah ok ill try
It's a function
Well, yes, a Date object is not a string lol
So TS will error
Try
mockDataStore().date.toDateString()
i owe you a coffee
4th time you helped me at least
Great! 😂 We solved it
No problem
ill pay it back with a youtube vid
I'm just usually on my phone and it's difficult to spell everything out
and credit ya
And write examples
so
<Show when={"date" in mockDataStore && mockDataStore}>
is vanilla typescript , or some solid black magic?Okay, so
"date" in mockDataStore
is just a regular javascript check, does this key ('date'
) exist in the object? And TS knows that if the property is in the object, then it isn't an empty object, so it must be of type StoreType
.
Now, when you have an expression "date" in mockDataStore && mockDataStore
, the &&
conditional ensures that the expression only resolves to mockDataStore
if the first condition passed, which means TS knows that mockDataStore
is of type StoreType
. If it doesn't pass, then the whole expression simply evaluates to undefined
.
That's the JS/TS part 👆
Now, you pass this expression to <Show>
's when={}
, and solid checks if it is undefined or not. If it is not undefined, then solid passes the value as a function to the callback. Solid also narrows the type (TS) of the value passed to the callback such that TS knows that the value is of type T
, not undefined.
There's no magic here, just solid doing some type assertions
But solid can only do that in callback formwhat should my setters be?
Hmm that's very interesting, probably something to do with how the TS types for the setter is defined in solid 🤔
You can do
setMockDataStore({companyName: e.currentTarget.value})
and TS won't complain
Though if you plan on setting properties separately, you might want to consider using createStore<Partial<StoreType>>
Which would mean TS would allow some properties to be set while others are not set
Which would also solve this problemthis the only reason I use stores atm)
i can't be the first to have the issue with stores
any examples of this?
this seems to allow single prop updates fine
not sure i need anything else
how would I use your way to replace this path way?
setCompanyDetailsStore('companyDetails', 'companyName', e.currentTarget.value)
this works