What is this even supposed to be used for? You can type cast to anything, but if your file is !strict you can only typecast to the classes above you or else you will receive a warning that to my knowledge, you cannot remove.
from my understanding, type casting is used to specify what exactly an object is, which allows you to use methods from that specific type. In java this makes sense to me, because your compiler will throw an error if you use a method from a subclass.
In roblox though, it doesn’t seem like that use case exist?
this is the only usecase I could find from the luau website, which is primarily using typecasting as a way to just typecheck the items in {} name. it doesn’t seem to serve any other purpose aside from ensuring that all name objects are of type string.
What use does it serve then? is it really just used to typecheck elements in a table? an example would be appreciated .
I often use it as a sort of note system to myself as well as a way to ensure I get as many auto corrects as possible. For example, in a script in startercharacter scripts, if you do
local humanoid = script.Parent:WaitForChild(“Humanoid”) :: Humanoid
you will see that humanoid.WalktoPoint is a typo, but you would not see this without the typecasting.
type casting in roblox does not limit what the variable can contain. It assist you in scripting as the scirpt editor will know what type/class the variable is
You could also do this with type checking though, which is why I’m confused. local humanoid : Humanoid = script.Parent.Parent:WaitForChild(“Humanoid”)
lets use the humanoid example. if i just set the variable without type casting, the script will not know what class/type the variable is which is sometimes a disadvantage as auto type will not work and i got to manually the property i want to get
so there is no functional difference between type-casting and type checking, it’s just different methods of casting a type to a variable? (either when instantiated or declared)
All fine, no type issues created, but because of this it’s easy to cast the wrong type to a function, so
--!strict
local a = 1 ::number
function plus(n:number) :number
return n+1
end
a = "1"
plus(a)
Should let the linter know a is being assigned the wrong type, before we run the code to find the error.
The : and :: would be better thought of as annotations rather than actually typecasting.
You can create custom types by
export type myType = {
var : string,
const : number
}
local a :: myType
But again it seems the main use is for reference to avoid a logic error, rather than forcing a type error.
But please someone correct me if I’ve missed something!
there are cases when Luau recognizes it as for example an Instance?, if you use strict typechecking it will give a type error if you try to simply add a type annotation. thats why you typecast to forcefully set it to Humanoid making it assume that it just exists, this only works if the types are related. you can type cast number? to number but you cant cast it to string for example