How to use type annotation in coding?

Hi there :waving_hand:
I have some problems that I can’t understand what the type annotation is in luau. Therefore, I want to ask you how to use type annotation and when should I use it?

Hi, @Dammio_6.

You mean these ?

local m={["USA"]=localtimehere}
local function GetLocalTimesOf(Country:string,Time:number?) -- this ?
if not time then print(m[Country]) else print(Time) end
end

if so then here two documentation by (offical roblox)

Type checking | Documentation - Roblox Creator Hub

Native code generation (Correct use of type annotations in native mode) | Documentation - Roblox Creator Hub

You use type annotation to ensure e.g. variables, arguments etc have the correct type to avoid errors and to know what something expects. Another benefit of this is getting autocomplete for certain types such as Instances.
This is often paired with --!strict written at the top of the script that enables strict mode.

Here are some examples of this:

local a: number = 3

This snippet uses a : to explicitly define the variable a as a number (This could be anything such as Instance, string, boolean, Part etc). Now if you try to set the variable to anything other than a number then you’ll get a warning.

"hello" :: boolean

This is called type casting. It lets you specify the type of a value. In this case, we are saying that the string "Hello" is a boolean. This could be used in specific cases where for example a value that’s supposed to have an explicit type has an any type.

local b: Instance?

This is the same as the first one, but you might have noticed the ? after the type. This question mark means that the value could possibly be nil. This could be useful in for example functions when you aren’t expecting an argument.

function add(a: number, b: number): number
    return a + b
end

This is a function with type annotation. you can use the : to define types for individual arguments as well as the return type (which is defined after the parentheses).

Here’s a really good article explaining this much better than me

1 Like

just to be clear for Luau, the type annotations are purely visual (they are ignored during runtime). adding a type annotation doesn’t prevent the code from running during incorrect cases; they just help you debug when/if those incorrect cases show up

Thank you. Now I can exactly understand what the type annotation is (or at least I think so)

1 Like

Also here is my little recommendation, if u wanna create table with strict type then write like this:

local my_table: {[string]: any} = {}
--                   ^      ^
--                         value type
--                 key type
--                            

ALSO you can create “custom types”, example:

type my_custom_type = {
  my_number_value: number,
  my_string_value: string,
}

-- in module scripts use:
export type my_custom_type = {--[[data]]}

-- you can use your "custom types" in functions or something like this
local my_table: {[string]: my_custom_type}

function my_func(arg1: my_custom_type) 

end

There are also generics, but I cant fit it into one post.

1 Like

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