I’m following Sean Bradley’s course on Threejs using Typescript.
I’m very new, but I feel like I’m finally starting to make sense of things.
However, occasionally when following others’ tutorials not using Typescript I sometimes run into problems.
Currently, I am trying to make a loading screen for my application.
What type of element is your progress bar? .value is only attributed to elements such as inputs or options, if you’re using a div you’ll need to set a style property such as left or another to the value of your progress
because once again, progressBar might be undefined but you’re reading from it which could crash (if progressBar couldn’t be found), which is what TS tries to protect you from. a simple check should be enough.
if (progressBar) {
progressBar.value = ((loaded / total) * 100)
if you know that progressBar cannot be null then you can force TS to just accept that with a ! and now you don’t have to check for its presence:
Thanks for taking the time to break this down and speak to TS’s conditions and needs.
You offer a great explanation, one that gets me thinking about wildcards and casting in an engine like Unreal.
I think I understand why this is happening in the first place, that TS is not in direct conversation with the HTML document and can’t guarantee the existence of the items I’m requesting.
Adding the “!” has satisfyied the call to “progressBar” but not “.value”
similarly, adding the “if (progressBar) {” satisfies progressBar but with the same error for “.value” continuing to claim property does not exist on type HTMLElement, and of course, why should it? It has no way of knowing what’s in the HTML document.
Do I need to offer a similar “!” specific to the .value?
I really don’t know if you intended on my including the last line, as I understood it to be your explanation that now progressBar.value could satisfactorily = anything
But its inclusion or exlusion didn’t change anything and I tried putting it in other places.
Like I said, I’m new to coding, more versed on node-based programming like Unreal’s Blueprints or Houdini, so I get into trouble with sequencing and proper inputs.