How would I type check for a specific set of allowed strings?

I’m trying to do something similar to what Roblox already seems to do with Instance.new():

image

It auto-fills for all the available instance types. Is there a way to efficiently do this, for example from a table of allowed strings?

The best I was able to think of was this:

function WorldFx:TriggerClient(name: "Fireworks" | "ButtonPush", ...)

But if I have 10 types for example? 100? This becomes a pain very quickly.

Any help or info is appreciated!

3 Likes

You could just make a module script and require it to export all they type from there. If you don’t want hundred of clutter type-check on your function parameter. An example follow by nicemike40 post: Exported types in Luau - #4 by nicemike40

Module script

export type name = "Fireworks" | "ButtonPush"
return nil

Your Module script

local types = require(path.to.ModuleScript)

local WorldFx = {}

local WorldFx:TriggerClient(name: types.name, ...)
      --things to do
end

return WorldFx

Happy type checking!

1 Like

You’re compounding two separate features. The typechecking is what you’re writing on the parameters and also what you’re seeing above the description of the function. The scroll list is part of auto complete; you can’t configure argument auto complete for your own functions.

3 Likes

Thank you @LerpSoh and @colbert2677!

I guess doing this just won’t be worth it… I wonder if Lua or Luau itself might come out with some special table-unpacking to use for types, though that seems unlikely.

Lerps example is probably the best-case scenario for doing this, if anybody sees this topic in the future. Otherwise, I’m just going to trust my own spelling abilities lol

1 Like

For typechecking, I suggest looking at this:

Thats my small input haha

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.