Type Annotation to make automatic union types?

It’s possible to automatically make yourself a type without having to type it all in type annotation.

local test = {
	a = function()
		return "value"
	end,
}

export type test = typeof(test)

 

Now for Union Types, like this.

export type test = "a" | "b" | "c"


function check(value: test)
	
end

the type checker thinks now that the value’s could either be, a,b,c as strings

And I was wondering how to turn this into a type check though.

local test = {"a", "b", "c"}

To achieve the same thing.

Not sure how. Not even sure if Generics can somehow be used to achieve this or not.

Not sure if I understand the question but if I wanted to type-check

local test = {"a", "b", "c"}

it would just be

local test: {string} = {"a", "b", "c"}

I do not believe there is a way to do this in Luau, no.

There is an issue opened in the Luau repo suggesting related functionality:

Your specific use-case would also require a way to mark an array-like table as sealed (or perhaps automatically detect table.freeze in the same way typescript has as const)

1 Like