How often should I type check?

Recently I’ve fallen in love with type checking. It’s probably one of the best updates for coders. However, how much type checking is too much? Should I type check every variable?

For example, is doing this too much? (type checking service variables that are basically constants)

local PlayersService: Players = game:GetService("Players")
local ServerStorageService: ServerStorage = game:GetService("ServerStorage")

I think you can type check some variables

Hi. If you’re talking about typing types literally, you don’t really need to do that in Luau. Luau has an automatic type inference feature, which means that you only need to place types for those values that may be ambiguous and cannot be easily inferred.

game:GetService("Players") returns a Players so PlayersService automatically has type Players.

1 Like

Avoid explicit types when you don’t need them to reduce noise, but in my personal opinion you you should declare them for function arguments and return types as they’re part of your signature. So like:

local function addTogether(x: number, y: number): number
    local sum = x + y -- Not sum: number
    return sum
2 Likes

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