What is "never"?

While looking through functions, and setting variables, I noticed a item called never
What is it? How do I use it? or am i not supposed to?

Example:

local console = {}

local function console.DoSomething(Var: never) -- What is this?
end

retrun console

Is it:

  • The opposite of any?

  • A Variable that isn’t called?


There are no details about it

1 Like

After doing some testing it appears to be a variable that is initialized as nil.

--!strict

local v = nil

function tst(var : never)
	print(var)
end

tst(v)

Though I can’t be certain that this is true so take this with a grain of salt.

But there is already one called nil:

local console = {}

function console.DoSomething(Var: nil)
	
end


return console

From Type checking - Luau

never is also said to be the bottom type, meaning there doesn’t exist a value that inhabits the type never . In fact, it is the dual of unknown . never is useful in many scenarios, and one such use case is when type refinements proves it impossible:

local function unknown(): unknown
    return if math.random() > 0.5 then "hello world!" else 5
end

local x = unknown()
if typeof(x) == "number" and typeof(x) == "string" then
    -- x : never
end
2 Likes

So in what case should i use it in? or do i not?

2 Likes

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