How to add anything to a type?

Hi, I don’t know how to explain it, so hopefully this code and the title is enough. Sorry!

type button = {
 Name: string,
 Cool: Boolean,
 …--Allows for anything to be added?
}

Any help is appreciated, thanks!

1 Like

To allow anything in a type, you should use ‘any’

Ex.

type button = {
 Name: string,
 Cool: Boolean,
 Property: any --Allows for anything to be added
}
1 Like

Sorry, I meant so that it there could be anything added any time, for example,

local function addSomething(button: button)
 -- these all add new stuff that wasn’t originally in the type
 button.Hi = true
 button.Hey = false
end
1 Like

That defeats the purpose of the type - if you want to do something like this, you may as well just use a dictionary.

1 Like

You can add a [string]: any to be able to add anything to something assigned that type:

type foo = {
    bar: number,
    [string]: any,
}

Alternatively, Luau automatically assigns types to unsealed tables you declare them with values.

1 Like

Yes! Exactly what I wanted! Thanks a lot!
(Again!)

Yeah, I was mainly doing this so that I can add functions to self, from all of the functions inside of the module

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