Fixing type errors

Currently trying to code while specifying types for autocomplete and less mistakes, wanted to know the solution and/or related videos that could be linked:

I’m trying to create a type that looks like this


and due to the way that I set the values, being that I set some of the values after I create a table with this type, it used to give me an error saying the config object was invalid
I’ve… fixed? the issue by making it possibly nil however this now makes it so that this error pops up
image
I don’t want to change it to just any type (destroying the whole purpose of this)
how would I go about fixing this, or am I simply using the whole type system incorrectly

Try changing all your | nil type statements to ?. It means the same thing, but it’s neater and I think the other expression might be messing with the Luau checker you are using. If that doesn’t work, paste it into an actual Roblox script and see if this type error still occurs.

--e.g.
number | nil
--changes to
number?

But can you send the other code that featured the previous issue before this fix?

Value of type ‘ImageLabel?’ could be nil

That error sounds like you’re trying to use the variable without checking that it isn’t nil first?

This is expected behaviour…

So if the type is wrong it will show you the error, if the type is nil it reminds you to check that the value is not nil before trying to use it in a way not compatible with it being nil (e.g. calling a method).
In your use case, I would set the type as ImageLabel? and then check it is actually an image label when I want to use it (e.g. if myConfig.right then)

Note: nothing errors in the Studio IDE, pretty sure its only an “error” in Luau LSP (my VSC extension) or maybe it errors when it runs, haven’t checked

Question mark did not change anything,
local newCircularIndicator: CircularIndicatorConfig = {
maximum = maximum,
prefix = prefix,
dataValueSource = targetParentObject.Data[prefix … “Value”],
damageDataValueSource = targetParentObject.Data[prefix … “DamageValue”],
}
this nets the error that its invalid for the type, lest I put the | nil
theres suppose to be a for loop that fills it in later

The value isn’t actually ever supposed to be nil, and all of the values for the config type should be filled in before any code using it is called, I only put | nil to “fix” the previous

Issue is, the value isn’t really ever uh meant to be nil and a whole bunch of errors are meant to happen basically; I’m not really planning on a check since the check should throw a bunch of errors anyway, which would happen even without it

just deleted the type specification because i do not know how to use this system

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.