Unsupported require path linting is confusing

Review the following:

--!strict

local function foobar(a: ModuleScript)
	local b: any = require(a)
end

This results in the require line being linted as “Unknown require: unsupported path”. Why and how exactly do I have it not pick that up?

My exact use case is a deserialiser that’s intended to accept a ModuleScript, require it and then break down the data it returns into an instance. Below is the exact relevant lines of code I’m using which served as a basis for the above repro:

function SoundSerialiser.Deserialise(rootScript: ModuleScript, parent: Instance?): Sound | SoundEffect
	local rootData: {[string]: any} = require(rootScript) -- Warning here

I am aware of the other thread discussing the same problem, but its replies are bloated with unhelpful information and the solution provides no closure besides assuming its a bug. It’d be hard to get informative replies by just rekindling that thread so I made this one.

2 Likes

There are some caveats here though. For instance, the require path must be resolvable statically, otherwise Luau cannot accurately type check it.

image

5 Likes

I’ve managed to find a workaround for this! (sorry for the bump)

In the section of the Luau type checking article you referenced in your solution, it mentions typecasting the result of require() to any in order to break cyclic module dependencies.

However, this also worked to remove the silly little “unknown require” lint. (the ModuleScript you are requiring also doesn’t need to be casted into a ModuleScript)

--!strict

local function foobar(a: ModuleScript)
	local b: any = require(a) :: any
end

And in my case, a strictly-typed object-oriented context with custom types set up, the linter basically re-casted it into the type it was meant to be.

image

Talk about a hacky workaround, but until the state of strictmode linting improves, I guess it’s all we’ve got. :person_shrugging:

1 Like