Skip to content

What is the difference between unknown and any types? #1846

Closed Answered by carsakiller
lua-rocks asked this question in Q&A
Discussion options

You must be logged in to vote

Assuming it is intended to mirror TypeScript:

any is a value that is permitted to be anything; number, boolean, table, function - anything. It is often considered bad practice to label something as any because it is then allowed to be whatever, and type checking will pretty much just ignore it, not providing any warnings - because again, it is allowed to be any.

---@param x any
local function double(x)
  return x * 2
  -- this is fine for a number, but what if someone passed in a boolean?
  -- you would receive a runtime error, but type checking won't warn you because x is
  -- annotated as accepting any type
end

I think the unknown type is relatively new to the LSP, but in TypeScript it …

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by lua-rocks
Comment options

You must be logged in to vote
1 reply
@carsakiller
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) feat/type check Related to the type checking feature
2 participants