如何在 Luau 中输入注释模块脚本?

问题描述 投票:0回答:1

我目前正在制作严格的类型检查脚本,但无法找到一种方法来类型注释需要模块脚本的变量而不使用类型

any
,这是不好的做法。

DoSomethingScript

--!strict

local moduleScript: any = script.SomethingModule -- How do I change the any type here?

moduleScript:DoSomething()
moduleScript:NotMethodAndShouldRaiseWarning() -- Using the any type doesn't give warnings for non-existant methods

SomethingModule

local SomethingModule = {}

function SomethingModule.DoSomething()
    print("Doing something")
end

return SomethingModule

我尝试查看 Roblox Creator Hub 文档Luau 网站了解如何键入注释模块脚本,但找不到任何相关内容。

那么我如何定义模块脚本的类型?

lua roblox luau roblox-studio
1个回答
0
投票

如果我是正确的,类型应该是

ModuleScript
返回的类型。在这种情况下,这将是一个
table
并查看 documentation 您可以像这样定义它的类型:

--!strict

local moduleScript: {} = require(script.SomethingModule);

moduleScript:DoSomething();
moduleScript:NotMethodAndShouldRaiseWarning();

您还可以像这样更具体地定义它:

--!strict

local moduleScript: {[string]: any} = require(script.SomethingModule);

moduleScript:DoSomething();
moduleScript:NotMethodAndShouldRaiseWarning();
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.