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.
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:
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
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!