Require() "Unkown require" when called with an indirectly referenced module

In a project I wish to dynamically require ModuleScripts that are a child of another ModuleScript:

for _, child in script:GetChildren() do
	if not child:IsA("ModuleScript") then continue end
	local tutorial = require(child)
	if tutorial.Name and tutorial.Enabled ~= false then
		tutorials[tutorial.Name] = tutorial
	end
end

This script is marked with --!strict, and hovering over the require() call gives a Type Error: Unknown require: unsupported path.
The argument is correctly typed as a ModuleScript but the typechecker doesn’t like an argument it can’t statically fetch.

This occurs with and without the new Luau type solver (which btw just completely fails on large place files so I can’t even use it).
Repro place file using FindFirstChildWhichIsA:
Unknown require bug.rbxl (57.0 KB)

Expected behavior

No type error should occur if it is passed a ModuleScript instance.

1 Like

This is an expected behavior of strict mode. Luau cannot statically infer this sort of highly dynamic behavior.

Your two options here are:

  1. Write out your requires manually. This is what I would do in your tutorial use case since otherwise it’s possible for you to make a script that doesn’t return a proper “tutorial” (with Name and Enabled) and get no static lints for it.
  2. Use (require)(tutorial), a trick to use require non-statically.
1 Like