Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with the table annotation.. #1655

Closed
frostedpenguin opened this issue Oct 25, 2022 · 7 comments
Closed

Problems with the table annotation.. #1655

frostedpenguin opened this issue Oct 25, 2022 · 7 comments
Labels
feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) question User has a question

Comments

@frostedpenguin
Copy link

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Linux

What is the issue affecting?

Annotations, Type Checking

Expected Behaviour

I am trying to use the table annotation with a custom string type to better define the key values of my dictionary..

This is what I have tried

--- @type table<"root", string>
local directories = {
	root = "root",
}
print(directories.root) -- 👈 this should not throw any warnings

Actual Behaviour

--- @type table<"root", string>
local directories = {
	root = "root",
}
print(directories.root) -- 👈 this throws Undefined field `root` warning

This also does not work

--- @alias themeDirectories "root"
--- @type table<themeDirctories, string>
local directories = {
	root = "root",
}
print(directories.root) -- 👈 this throws Undefined field `root` warning

Reproduction steps

run the code snippets ⌨️

Additional Notes

No response

Log File

No response

@firas-assaad
Copy link
Contributor

Can't you use --- @type { root: string }?

@frostedpenguin
Copy link
Author

frostedpenguin commented Oct 25, 2022

This notation works fine although there are some inconveniences that come with it.

  • If the table becomes really big it's hard to break it up and you are forced to use a single line for the whole type.
    Which is a bit hard to maintain and looks weird.

  • with the table annotation you can effectively reuse aliases which makes code reusability easier.

@carsakiller carsakiller added the question User has a question label Oct 28, 2022
@carsakiller
Copy link
Collaborator

Hello! 👋

Have you tried documenting with @class?

It would work like this:

---@class directory
---@field root string

---@type directory
local directories = {
	root = "root",
}
print(directories.root)

Is there anything I can improve with the documentation that was unclear?

@carsakiller
Copy link
Collaborator

@frostedpenguin has your issue been resolved?

@frostedpenguin
Copy link
Author

Yes I think it makes sense.. maybe there should be a note in the docs that the table annotation can only have primary types as its keys..

@sumneko
Copy link
Collaborator

sumneko commented Nov 13, 2022

Should be fixed now.

@carsakiller
Copy link
Collaborator

Yes, it appears that, at least in v3.6.2, the following works:

--- @type table<"root", string>
local directories = {
	root = "root",
}

print(directories["root"])

However, the following is also permitted, should you not want to use ---@class:

--- @type {root: string}
local directories = {
	root = "root",
}

print(directories.root)

There are often many ways to document things, and you can pick your favourite for your use case 🙂.

@carsakiller carsakiller added the feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) label Nov 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) question User has a question
Projects
None yet
Development

No branches or pull requests

4 participants