Logo with title ".Blog"

Type smuggling

Published on

Type smuggling:

type T = typeof(require(...))

Allows you to get types from a module without requiring it. Does not work with exported types.

To get around exporting, types can be smuggled through the returned table.

local export = {}

-- Innocent module stuff.
function export.new()
end

-- Some types. Must require
-- the module in order to get.
export type Foo = string | number
export type Bar = () -> boolean

-- Smuggle them through
-- the returned table.
export._Foo = (nil::any)::Foo
export._Bar = (nil::any)::Bar

return export

This is incredibly useful for smuggling types from a server module in a client module so the data the client is going to receive from the server via a remote can be fully typed.

@fewkz, 6:31 AM · Nov 9, 2022

So nothing breaks on the client even though the modules containing the server types aren’t replicated, because types don’t matter at runtime. Interesting approach.