Skip to content

Generic indexed access T[K] resolves to any #986

Closed

Description

@sudo-tee

I'm trying to type a generic state accessor where the return type depends on the key:

---@class Foo
---@field cost number
---@field name string

---@type Foo
local FooObj = {
  cost = 100,
  name = 'example',
}

---@generic K extends keyof Foo
---@param key K
---@return Foo[K]
function get(key)
  return FooObj[key]
end

local x = get('cost') -- hovering x should show: number, but shows: any
Image

Is this a pattern supported by emmy lua, or am I missing feature that would allow me to do something like this ?

Thanks a lor for your help

Activity

  1. CppCXY commented on Mar 16, 2026

    @CppCXY
    Member
    ---@class Foo1
    ---@field cost number
    ---@field name string
    
    ---@type Foo1
    local FooObj = {
      cost = 100,
      name = 'example',
    }
    
    ---@generic K extends keyof Foo1
    ---@param key K
    ---@return std.RawGet<Foo1, K>
    function get(key)
      return FooObj[key]
    end

    The handling for the Foo[K] syntax has not been implemented yet.

  2. self-assigned this
    on Mar 16, 2026
  3. sudo-tee commented on Mar 16, 2026

    @sudo-tee
    Author

    Thanks seem to work great as an alternative for the moment.

  4. linked a pull request that will close this issueupdate #988on Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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