Skip to content

[Feature request] Implicit casts for common conversions #3027

@emmericp

Description

@emmericp

I got some code for parsing command line flags/user inputs that commonly uses patterns like this

---@return string ...
local function strsplit(str, sep) end

local a, b, c = strsplit(userinput, ",")
b = tonumber(b) or 0 -- error:  This variable is defined as type `string`. Cannot convert its type to `number`

The fix is to define b as string|number in the declaration, but it's a bit cumbersome, especially if I just want b as a number:

local a,
---@type number|string
b, c = strsplit(userinput, ",")

So it would be nice if common type conversion patterns like this could be recognized automatically and either allow the cast or infer the type as string|number from the beginning.

Edit: See comment below, the implicit cast is already happening, this is really about type inference based on later assignments. Which may or may not be a good idea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions