Why does typescript not accept `text() && text().length === x` in solid-start?

Is anyone aware why typescript complains about
text() && text().length === 6
text() && text().length === 6
in a project with the tsconfig.json that comes with the solid-start template? (E.g. typescript complains I should use text()?.length but it didn't in other projects with solid.js) I swear I had solid.js applications where the ts compiler correctly deduced that I checked that text() is non-falsy? EDIT: "typescript": "^4.9.4"
6 Replies
apollo79
apollo7914mo ago
The problem here is, that a function can return something different for each call, so ts won't allow this.
Bersaelor
Bersaelor14mo ago
ok, so probably the ts compiler is just broken in my other ts project yeah, gotta use text()?.length === 6 then
apollo79
apollo7914mo ago
There are multiple solutions to this afaik: 1. You can assign the result to a variable and check it 2. You can use the non-null-assertion operator ! 3. You can write a check function with return type T is ... Or yes, you can use ?. 👍
Bersaelor
Bersaelor14mo ago
1. You can assign the result to a variable and check it
Yeah, thats what I usually do, it's just so verbose compared to other environments. In swift for example we can do
if let value = someOptional() {
use(value)
}
if let value = someOptional() {
use(value)
}
where in ts I have to do
const value = someOptional()
if (value) {
use(value);
}
const value = someOptional()
if (value) {
use(value);
}
apollo79
apollo7914mo ago
Yes, that is true But I see no problem with using the non-null-assertion operator if you checked it before Or writing a check function if it feels less verbose
bigmistqke 🌈
bigmistqke 🌈14mo ago
I have this utility function I use sometimes when(signal()).then(signal => ...)
Want results from more Discord servers?
Add your server
More Posts
Universal renderer createElement isn't runningI'm working on a custom renderer and I can't get the createElement function to run. I'm not sure if How to read reactive state from imperative contexts (audio/music)**tl;dr:** is there any major DO NOT guideline around (constantly!) reading reactive states from fuHow to handle auto scrolling in a chat app when new message comes in?So this is an example what i currently have ```jsx const App = () => { const [messages, setMessageHow to get `onMount` to be called on the client for a server-rendered siteI've set up a new project using `solid-start` with `solid-start-aws`. Running `npm run build` createReactivity with normal functionsI try the following: `doubleCount` is using `createMemo` while `tripleCount` is a normal anonymous fSolid-js design system CSS troubleshootingI have a design-system I am building out using Kobalte. However, when I export the components none oHow to throw an Error on an unknown route?I define the content for each route like so: ```jsx return ( <Routes> <Route path={'/foo'} eleJoining SolidJS Vite app with a rest api as single appHello, I'm building a simple front end with the default SolidJS+Vite template. I call this the clienHow to make a sticky navbar change color when it has reached the top? Like in solidjs homepageI think I should use this https://github.com/solidjs-community/solid-primitives/tree/main/packages/iHow to detect env in `createServerData`Hey there, during development I like to run `npm run dev` on my site, so I can render it locally in