Pre-defining require as a variable breaks returned modules types.

Defining require as a variable forces all returned modules to be returned as any, even if it is a string, function or a table.

With require pre-defined:


Without require pre-defined:

Expected behavior

For returned modules types to remain the same regardless if require is pre-defined or not.

Does it work if you type it as local require: typeof(require) = require? Also, why are you overwriting it with a variable?

Do you mean to say that you are overriding the require function itself?

1 Like

I meant essentially this, so yeah.
local require = require
This is what breaks the returned modules types.

Got it.

This is by design. Luau can’t be sure exactly what you want to happen in this case, so it has to conservatively assume that your custom require is just an ordinary function whose name happens to coincide with a builtin.

1 Like

Are there any workarounds on that or plans to change that?

The most common workaround I know right now is to use typeof to capture the type of the module.

--!strict

function special_require() : typeof(require(script.Parent.MyModule))
	return (nil :: any)
end

local a = special_require()

Longterm, our goal is to adjust the behaviour of the builtin require function so that it isn’t necessary to override it.

1 Like