luau-lang / luau

A fast, small, safe, gradually typed embeddable scripting language derived from Lua
https://luau-lang.org
MIT License
3.8k stars 352 forks source link

export type destroys free table state type syntax declaration #1189

Closed karl-police closed 3 months ago

karl-police commented 3 months ago

export type test = typeof(table.clone())

This is supposed to give you a Free Table {- -}, as shown here:

image

But it doesn't

Testing Script

local module = {}

export type test = typeof(table.clone())

return module
local typeTest = require(script.ModuleScript)

local test = {} :: typeTest.test

test.e = "e"
test.

image

vegorov-rbx commented 3 months ago

Modules exports are immutable and all free tables are sealed on that boundary.

karl-police commented 3 months ago

Modules exports are immutable and all free tables are sealed on that boundary.

Even happens with unsealed tables turning into sealed tables, not only free tables.

Removing the possibility to declare that there's a table which's content is not yet completly fully known.

For a table like

local module = {}

module.Data = {}

return module

if you say Data is unsealed and access to that can be modified, but the type export won't allow it, where I start to not understand if this is intended to be like that or if that can be something that can be changed on Luau.

AmaranthineCodices commented 3 months ago

To explain further: both free and unsealed tables are implementation details of the type solver. They are intended to go away when solving is "finished" for a module, which explains the behavior you're seeing here. You should not rely on them being exported through module interfaces, and we have no plans to support that. That you see the free table in Roblox Studio's hover type view is the result of an optimization - we don't sanitize interior types for performance reasons.

Luau generally treats each module as an independent unit, require calls excepted. This means that Luau isn't going to think about the possibility of module.Data being changed after the module is required. Supporting this in a way that's up to Luau's standards would require making judgements about the order in which each line of code in the program runs, which is generally impossible.