Skip to content

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

@sudo-tee

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

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