Add support for number literals

Currently, there is no way to define number literals with Luau. Like existing string and boolean literals, number literals can come in handy for the same reasons. For example, with string literals, you are able to differentiate types by refining the type with conditionals:

-->> example string literal usage

type objectA = {
    name: "Apple",
    healAmount: number,
}
type objectB = {
    name: "Car",
    drivingSpeed: number,
}
type union = objectA | objectB

local var: union = --> some variable
if var.name == "Apple" then
    --> "var" now has type "objectA"
else
    --> "var" now has type "objectB"
end

This use case can be extended to numbers, which can be very useful in cases like datastore versioning:

-->> example number literal usage

type v1 = {
    version: 1,
    Apple = 1,
    Banana = 2,
}
type v2 = {
    version: 2,
    inventory: {
        Apple = 1,
        Banana = 2,
    },
}
type union = v1 | v2

local data: union = --> some variable
if data.version == 1 then
    --> "data" now has type "v1", which the proceeding code can interpret and convert to "v2"
end

Languages like TypeScript have number literals. This is an important next step in furthering Luau.

10 Likes

Yup completely agree, i wonder why they chose to not support it.

1 Like