Typechecking issue

Hello! Very simple question: Is there a way to typecheck a parameter to any, except certain types are excluded/not accepted?

I am currently making a Stacks and Queues data structure implementation in Luau, and for the function push, it requires a parameter which can be any, except the nil value. My issue is on how to tell the code to exclude the nil type? All help is appreciated.

I think you should change interference mode at the first line of the script, you can read it in
Type Checking Documentation, read part of interfernce mode

These only manage if the typechecking is strictly checked, throws out an error, etc. I don’t think they add new functionality.

Maybe it will work

any & ~nil

No, it errors. Your comment is still appreciated it though!

It’s possible with type functions

2 Likes

Luau Typechecking doesn’t support Unary Operators (like ~nil), u can still use Binary Operators tho.
idk maybe try to use this:
assert(v ~= nil)
in this way u ensure the typechecker that v isn’t nil

interesting, this looks promising. although, my friend did give me another simpler solution : the unknown type. It’s a type that defines that something exists and is not nil. And it’s safer than nil, too. Although I won’t mark this as a solution even if it is, I will give that title to my friend who recommended the unknown command since it’s much more practical.

I thought about it doing this in the actual function instead of typechecking it. Good suggestion!

This is not true. The unknown type is defined as the union of all types, including nil. It does not work.


Hmm. That’s true apparently. I also tested it, and it indeed does not show nil. I’ll reconsider the approach. If unknown is the top of all types, what is the difference between any and unknown?

If you do plan on using type functions like I suggested, I recommend brushing up on the type annotation syntax and all of its specifications.

P.S. I was able to somewhat simplify my original solution using types.singleton. However, because it’s still a type function, you are required to use the angle brackets.

Ah, ok! Now it’s much clearer to understand. Well, I guess I’ll give you it after all. Thanks!

while luau doesn’t support strict “exclude nil” type annotations like some other languages, you can achieve this behavior with runtime validation. you’ll need to manually check for “nil” where necessary in your functions and filters.

if you’re working with more advanced data filtering or type assertions, you’ll likely need to add those checks explicitly in your codebase, and that’s usually the safest and clearest way to handle such cases in luau

I agree and it’s indeed a safer approach. However, type checking at analysis time is still many times faster at catching problems because type errors jumps at you immediately, instead of having to playtest and debug. They’re both valid and they should both be used :slightly_smiling_face:

I’m just a bit confused by what the .negationof function is

Everything you need to know is in the Luau language documentation I linked above