How to create new types in luau?

Let’s say I want to create a new type called “Human” that is a dictionary with the following properties:

local Human = {Health = number, Height = number, Name = string, etc.)

How can I convert it to a type so I can do the following:

function setHumanHealth(human: Human, health: number)
	--Health should auto-complete
	human.Health = health
	--this should redline:
	human.Test = "Hello world!"
end

Currently, I’m limited to defining those as {[string]: any} but that isn’t efficient and doesn’t have the intended behavior, it just limits the input to dictionaries in general(else it redlines).

1 Like

For your current case, you’d want to define your type as:

type Human = {
    Health: number,
    Height: number,
    Name: string,
    --...
}

And then use it as so:

function setHumanHealth(human: Human, health: number)
    --...
end

You can’t make a custom type error for invalid indices, however, studio will give you an orange/yellow underline letting you know that the index or value is incorrect

3 Likes

I refuse to believe it was that easy.

1 Like

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