You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think it would be useful to have something like a Require<T>-type. This type would take a literal string type T and evaluate as the same type that calling require(modname: T) would. This way, a wrapper around require could easily be built like this:
--- Require a module, throwing an error if it returned `true` (meaning that you--- probably forgot to actually return something from it).---@genericT:string---@parammodule_nameT---@returnRequire<T> module -- <-- Note the use of `Require<T>`localfunctionchecked_require(module_name)
assert(type(module_name) =="string", "The `module_name` must be a string!")
localmodule=require(module_name)
assert(
module~=true,
string.format(
"Could not import module: `require(%q)` returned `true`!",
module_name
)
)
returnmoduleend--------------------------------------------------------------------------------localsocket=checked_require("socket")
localsocket_require=require("socket")
assert(socket==socket_require)
The type-hint of require() could then be changed to the following:
---@genericT:string---@parammodule_nameT---@returnRequire<T> modulefunctionrequire(module_name) end
The language server already has a way to make global functions behave like require built-in, in the form of the "Lua.runtime.special" setting. This type would essentially do the same, except that it would be more powerful / broadly usable, since it could be applied to any variable. It would also be easier to use, since it's just another type in your code instead of something hiding in a config file.
The text was updated successfully, but these errors were encountered:
SkyyySi
changed the title
[Feature request] A pseudo-type for custom require-like functions
[Feature request] Add a pseudo-type for custom require-like functions
Dec 22, 2024
I think it would be useful to have something like a
Require<T>
-type. This type would take a literal string typeT
and evaluate as the same type that callingrequire(modname: T)
would. This way, a wrapper aroundrequire
could easily be built like this:The type-hint of
require()
could then be changed to the following:The language server already has a way to make global functions behave like
require
built-in, in the form of the"Lua.runtime.special"
setting. This type would essentially do the same, except that it would be more powerful / broadly usable, since it could be applied to any variable. It would also be easier to use, since it's just another type in your code instead of something hiding in a config file.The text was updated successfully, but these errors were encountered: