Why do people use type checking?

Hello, I know what type checking is but I dont understand why people use it.

For example:

local function(Value : string,  SecondValue : number)

and

local CoolNumber : number = 2
local CoolString : string = "Hello"

Why do people do this? I have been scripting for a few years and I see a lot of people use type checking but I dont understand why.

I have never had to use it. Can someone give me some insight please and thank you :slight_smile:

2 Likes

If your scripting alone it doesn’t change much on your side but if your working with a team or making an opensourced module it definitely does help the person identify what they should actually be passing through.

4 Likes

typechecking is used for autofill and better debugging and makes ur code clearer

3 Likes

The type analysis is useful for knowing what goes where and what type a variable/parameter is. It also provides autocomplete and autofill for that variable.

It’s similar to typing game. and getting options

Without types:

local function foo(var)
   var. -- luau doesn't know what this could be, so nothing appears
end

With types:

local function foo(var: string)
    var. -- luau knows 'var' is a string, so it will give you options from the 'string' library
end

Like @HACANTERO said, it makes your code cleaner because you can see whatever is at a glance as opposed to searching through your code to figure out what type a variable is.

Additionally, passing invalid types will tell you that it’s wrong:

image


Another reason is the fact that you can define your own types for your code. For example, I defined a custom type to repesent PolicyService information for a given player

export type PolicyServiceInfo = {
	AreAdsAllowed: boolean,
	ArePaidRandomItemsRestricted: boolean,
	AllowedExternalLinkReferences: {string},
	IsEligibleToPurchaseSubscription: boolean,
	IsPaidItemTradingAllowed: boolean,
	IsSubjectToChinaPolicies: boolean
}

local function getPolicyServiceInfo(player: Player): PolicyServiceInfo
   --...
end

-- later in the script
getPolicyServiceInfo(PLAYER). -- every property defined in 'PolicyServiceInfo' will appear here
2 Likes

Primarily

It stops syntax and logical errors before they happen, allowing for big systems to be edited and maintained more easily, and it helps you to not forget to do .Value for a specific variable, helping you to be conscious of your code while your programming so it’s not a hassle to fix!

4 Likes

Ohhh wow, this makes a lot more sense now. Its pretty cool, I might start using it.

Thanks for the detailed reply and I appreciate all of the other replies.

Have a nice day/night :pray:

I just saw the edit, thanks for the extra info.

I have always wondered what export does. I’ll have to do a bit more research on that but thanks for telling me about this

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