Skip to content

Union type parameter causes "param-type-mismatch" warning #1790

New issue

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

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

Already on GitHub? # to your account

Closed
ArmoredPony opened this issue Dec 27, 2022 · 2 comments
Closed

Union type parameter causes "param-type-mismatch" warning #1790

ArmoredPony opened this issue Dec 27, 2022 · 2 comments

Comments

@ArmoredPony
Copy link

Here is the code that causes warning:

function add_month(time)
  local dt = os.date('*t', time)
  dt.month = dt.month + 1
  return os.time(dt)
end

And the warning itself:
image
The code seems to be working. Apparently, the extension understands that the type of dt variable is either string or osdate. It also knows that os.date function takes ostime as an argument:
image
It assumes that dt is a string and raises the warning but ignores the fact that it's also an osdate. Is this a bug or am I missing something?

@carsakiller
Copy link
Collaborator

This is due to os.date returning either a string or table. It may be possible to provide an overload so that unless *t is provided as the first parameter, the return type is string... but that may not be possible right now.

In the meantime, you can @cast the return of os.date to a different type:

local valueIKnowIsOsDate = os.date("*t", time);
---@cast valueIKnowIsOsDate osdate

@ArmoredPony
Copy link
Author

Ah, so I have to specify the exact type that belongs to this union. That makes sense I guess, thank you.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants