JohnnyMorganz / luau-lsp

Language Server Implementation for Luau
MIT License
235 stars 59 forks source link

false type-error for a custom type definition #629

Closed ActualMasterOogway closed 2 months ago

ActualMasterOogway commented 4 months ago

the type definition:

getproto: (f: Function | number | ProtoProxy, index: number?, activated: boolean?) -> { [number]: (() -> any) } | (() -> any) | ProtoProxy,

the type error: image the type error prevention: image

is this supposed to be that way?

JohnnyMorganz commented 4 months ago

it looks like a valid error to me, if getproto returns the function (() -> any), it cannot be indexed?

ActualMasterOogway commented 4 months ago

it looks like a valid error to me, if getproto returns the function (() -> any), it cannot be indexed?

getproto either returns a function (() -> any), a table containing functions { [number]: (() -> any) } or a ProtoProxy (which is a custom class, wont go into detail since it has nothing to do with the issue)

so it CAN be indexed if it returns a table, it should return a table when Argument 3 is set to true

JohnnyMorganz commented 4 months ago

Yeah, the point is that it could return either of those 3 things, but Luau doesn't know which one gets returned. If it returned any of the other parts of the union type, then it would lead to a runtime error. So you get a type error here.

it should return a table when Argument 3 is set to true

Luau doesn't know that, you haven't encoded it in your type.

You could use a function overload that checks if the 3rd argument is true and then only return the table type:

type GetProto = ((f: Function | number | ProtoProxy, index: number?, activated: true) -> { [number]: (() -> any) })
    & ((f: Function | number | ProtoProxy, index: number?, activated: boolean?) -> { [number]: (() -> any) } | (() -> any) | ProtoProxy)

In any case, this is not a Luau LSP issue. It is a valid type error. If you have incorrect type errors, I would recommend reporting them to Luau directly - this repository is only a language server implementation.