Luau Type Checking Release

Found another issue with type intersections, it is not possible to index any key of a variable.
This issue does not require strict mode for it to throw an error.
Uses the example that is provided in the documentation.

Code
--!nonstrict

type XCoord = {x: number}
type YCoord = {y: number}
type ZCoord = {z: number}

type Vector2 = XCoord & YCoord
type Vector3 = XCoord & YCoord & ZCoord

local vec2: Vector2 = {x = 1, y = 2}        -- ok
local vec3: Vector3 = {x = 1, y = 2, z = 3} -- ok

-- W000: Type 'XCoord & YCoord & ZCoord' does not have key 'x'
print(vec3.x)
-- W000: Type 'XCoord & YCoord & ZCoord' does not have key 'y'
print(vec3.y)
-- W000: Type 'XCoord & YCoord & ZCoord' does not have key 'z'
print(vec3.z)

Edit: My current work around is to redefine the type in full with intersection applied manually, this of course avoids the use of & and the errors stemming from it.

2 Likes

If someone overrides a type which luaU has defined by default then the errors it produce can be confusing, same for if someone has a variable name that maches a luaU type.
For example:

--!strict
type typ = {
	number:number
}
local number = {}
local test:typ = {
	number = number
}

type typ2 = {
	boolean:boolean
}
type boolean = {}
local test2:typ2 = {
	boolean = true
}

While i doubt anyone would do this on purpose, if someone was to accidently do this it could be confusing.

newproxy() says that it needs 1 argument even though it should be optional
setmetatable() does not support newproxy() as the first argument

1 Like

setmetatable doesn’t support newproxy during runtime though?

Oh yeah, I messed up XD.
I apparently forgot what newproxy was.

With the inability to use typeof without causing internal errors, and not liking the setmetatable work around I found, I continued to look for a type safe way to produce class prototype structures. This post is a warning to always make backups and how luau type checking is still unstable.

This work has permanently corrupted our place without any recent backups, and reverts are ineffective.
I had a module script in ReplicatedFirst named ModuleScript which I was playing with.
Everything was working fine until one change instantly crashed studio.
I am currently trying to recreate this change in a separate place.

Story Details

As normal I opened the recovery file and copied the last commited script to my clipboard.
I opened the original place and found that no other commits were lost so I deleted the recovery file.
I then attempted to open the module script to copy in the recovered version, it crashed when it opened.

The next time I opened studio, I was not presented with an option to recover so proceed straight to the original place.
The place was just as I had it before the crash, but I noticed I had uncommitted changes for ModuleScript.
Thinking this might be the cause of the crash I pressed commit, instant crash.

Again no option for a recovery, but now any attempt to open the place would mean cause a crash.
I thought it was because I had the module open in the editor, so asked my co-dev to open the place and delete the module script.
He was unable to open the place, crashing as well, at this point we attempted to revert the place.

First we reverted to before the last commit, and the place still crashes on open.
Second we reverted by several days, the place still crashes on open.
Our last option is to load a backup from a month ago.

Before we load our manual backup we want to let roblox staff look at this issue
We can not provide an rbx file as we cant open the place to save it, our place id is 4559239318
Here is a paste bin of the version I had on my clipboard, this one does not crash studio.

Studio crashing for this specific place occurs 100% of the time and it continues to crash even with rolling back the versions. My OS and my other co-developers OS are both Windows 10.

(Please can this be moved to Studio Bugs)

1 Like

Thanks for the report, we’re going to fix this specific crash. Note however that type checking is independent of the Studio version update / autosave / backup mechanism; what’s crashing Studio in your case is just the contents of the problematic script, and rolling back the place to the version before that script was introduced should make it possible to open again.

1 Like

I have now been able to reproduce the crash consistently. Paste the following code into a new module and then uncomment line 42. Studio will then crash. I am running windows 10 and have tested this code on a place which has no other scripts. The reason I believed it was a luau issue is because the problematic code is mostly type definitions and setmetatable calls.

Problematic Code
--!strict
-- Paste into a new module and uncomment line 42 to crash studio

----- Module Init -----
-- Generic module init

local GenericModule = setmetatable({}, {
	__index		= function(_, key) error(tostring(key)..' is not a valid member of Module', 2) end,
	__metatable	= function(_) error('The metatable is locked', 2) end
})

type LockedMetatable<self> = {
	__index: (self, any) -> (),
	__metatable: (self) -> ()
}

----- Prototype Init -----
-- Generic prototype init

local GenericPrototype = setmetatable({}, {
	__index		= function(_, key) error(tostring(key)..' is not a valid member of Generic', 2) end,
	__metatable	= function(_) error('The metatable is locked', 2) end
})

GenericPrototype.__index     = GenericPrototype
GenericPrototype.__newindex  = function(_, key) error(tostring(key)..' is not a valid member of Generic', 2) end
GenericPrototype.__metatable = function(_) error('The metatable is locked', 2) end

type GenericMembers = {
	Name: string,
}

type GenericMethods<self, prototype> = {
	__index: prototype,
	__newindex: (self, any) -> (),
	__metatable: (self) -> (),

	Foo: (self, number) -> ()
}

type GenericPrototype = typeof((function()
	-- local tbl: GenericMethods<Generic, GenericPrototype>
	local mt: LockedMetatable<GenericPrototype>
	return setmetatable(tbl, mt)
end)())

export type Generic = typeof((function()
	local tbl: GenericMembers
	local mt: GenericPrototype
	return setmetatable(tbl, mt)
end)())

----- Prototype Function -----
-- Generic functions for the generic prototype

function GenericModule.NewGeneric(name: string): Generic
	local newGeneric = {
		Name = name
	}

	return setmetatable(newGeneric, GenericPrototype)
end

function GenericPrototype:Foo(x: number)
	return
end

----- Module Return -----

return GenericModule

So I am just now starting to get into more luau stuff and was wondering a few things that I couldn’t figure out based on reading everything in this thread.

The first question is, is the type for the function arguments suppose to limit the argument to that type, returning an error if its not? because right now I am finding it treat args as anything. if you classify them as a string or number and pass through a boolean, it goes through anyway. Or am I mistaking on how these types work.

The second question is, what would the Player’s type be? Would it be player: Player, or player: Instance, or player: Userdata, or player: Object… im not sure

Would be nice to have a list of types and their use cases! :smiley:

I’ve tried several different ways but I can’t seem to figure out how to refine an Instance into a BasePart as typeof returns Instance when applied to a Part. What is the recommended way to refine, say, the Parent property of an Instance to a BasePart to be able to access its Position property in strict mode?

2 Likes

@zeuxcg I was scrolling through the beta list and noticed this entry has no “ⓘ” with linked thread, which means if I was not yet familiar with type-checking from keeping up announcements I would not know what this means:

Maybe this beta entry should link to this topic.

1 Like

I’m wondering too, glad someone else has the same issue :sweat_smile:

The following class will give the error “W000:Free types leaked into this module’s public interface. This is an internal Luau error; please report it.” when trying to export its type for use in ModuleScripts requiring it.

--!strict
local Class = {}
Class.__index = Class
function Class.new(): Class
	local self = setmetatable({},Class);
	self.State = false;
	return self;
end
function Class:Modify(): nil
	print(self.State)	--Comment out this line or remove '.State' and the error goes away
	return;
end
export type Class = typeof(Class.new());	--Remove 'export' and the error goes away

Have this error showing up when I checked up on a script, went to the internet and found this post. It seems the type checking doesn’t recognize the fact that the ObjectValue ‘Projectile’ has an assigned value, which is a part, and simply throws an error saying nil does not have the Clone function. image

Thanks for the report! I’m working on stuff related to this, I’ll add this example to my queue. And thanks for producing a minimal example, it’s very nice to work with.

1 Like

image
W000: expected a string or number, got number | string
Can be done using the below function

function a(b: string|number)
    print(""..b)
end
1 Like

I’d like to see generics available in functions, and default values for generics (default parameters would be good too; e.g. function blah(param: string = "roblox") ... end)! I feel something like this should be possible:

function GetValueOrDefault<T = any>(parent: Instance, name: string, defaultValue: T): T
	local child = parent:FindFirstChild(name)

	if child then
		return child.Value or defaultValue
	end

	return defaultValue
end
local possiblyNil = GetValueOrDefault(workspace, "HelloValue")
local alwaysString = GetValueOrDefault<string>(workspace, "WorldValue", "World")

Also the linter could do with some more work. It doesn’t seem to like ternary operations (or when you do the same with if/if-else statements), such as:

return function(val: number?): boolean
    val = type(val) == "number" and val or 0
    val += 1 -- W000: Type 'nil | number' could not be converted into 'number'

    return val >= 10
end
3 Likes

Add tostring().
Try use music.SoundId = "rbxassetid://" .. tostring(id) instead.

Looks like there’s been a regression in the typings for RemoteEvent.OnServerEvent.

The following code is benign, and used to emit no warnings but will now emit a warning:

--!strict
local x: RemoteEvent = Instance.new('RemoteEvent')
x.OnServerEvent:Connect(function(player: Player, shouldNotBeTrusted: any)
end)


'Type (Player, any) -> ()' could not be converted into '(Instance, any) -> nil'

1 Like

Found another regression: script is now typed as a ModuleScript instead of any, and this means you can’t directly access children of the script, even in scenarios where they’re 1000% guaranteed to exist at runtime!

local gui = script.GuiTemplate:Clone()

Yes, I am still in the camp that says unnecessary calls to FindFirstChild is bad practice and makes your code more verbose than it’s supposed to be. Besides this… aren’t we supposed to be able to rely on relative paths to require modules? Interestingly enough, dot-accessing within a require statement is allowed, but for some reason dot-accessing in any other context is not allowed?

This makes me think this is not an unintentional regression, and instead just intentionally poor design…
… which makes me intentionally put ugly statements at the top of my script just so I can work around the type system.
image

3 Likes