Skip to content

Table of tables of functions #1332

@muppet9010

Description

@muppet9010

As requested creating an issue for my question on how to approach this situation. Guessing there is no way currently given I was asked to create an issue :)

Discussed in #1331

Originally posted by muppet9010 July 14, 2022
I have the below concept and aren't sure how to go about properly type defining it. I have it as below and the typing almost works fine, apart from within the inner functions the parameters aren't getting their type.

---@alias EntityTypes table<string, EntityTypeDetails>

---@class EntityTypeDetails
---@field GetEntityName fun(name: string, count: integer): string

---@type EntityTypes
local EntityTypeDetails = {
    tree = {
        GetEntityName = function(name, count)
            return name .. tostring(count)
        end
    },
    rock = {
        GetEntityName = function(name, count)
            return name .. tostring(count + 1)
        end
    }
}

local x = EntityTypeDetails["tree"].GetEntityName("grey", 5)

So the function knows its types:
image

But the parameter doesn't:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions