How to create a type check to force arguments in a function to be a table?

I’d like to force arguments in a function to be a table:

function x(a: table)
	print(a)
end

x("string")

However, no errors are being shown.
Actually, I’m getting no errors for any type check, even I change it to number, etc.

What’s wrong?

There is no generic table type in Luau, so something like a: { [any]: any } will do.

Tks, but actually, as I said, I’m getting no errors for any type check, even I change it to number, etc.
How do I make type check work?

Put --!strict at the top of your file.

1 Like

Still no errors when running or in the Expressive Output:

--!strict
function x(a: number)
	print(a)
end

x("string")

Luau just screams at you at lint time, not anything in the output. It isn’t meant to prevent code compilation or throw an exception. You might be thinking of other language like C++ where this would be the case, but it probably depends on the compiler/linter/etc.

1 Like

Well, at what time and where will I be alerted? Because so far I don’t see anything.

At compile time.

If the check is required at run-time, you can use the assert function:

assert(typeof(a) == "number", "Expected a number!")

I believe that is the proper usage.

1 Like

Ok, but for some reason, I’m getting nothing:

Do you have the new script analysis?
image

1 Like

Oh, only with this beta?
I gave up using it, because it is buggy, full of wrong alerts, which hinder more help …

1 Like