#is there a bug with the type checker??

1 messages · Page 1 of 1 (latest)

narrow sand
#

here i have this type

type AI = {
    Attack: (Player) -> (),
    Reload: (number) -> ()
}

when it has a primitive type, it works fine

local enemy: AI = {}
function AI.Reload(event)
  -- event is number
end

but if it has a non primitive type, event results in unknown

local enemy: AI = {}
function AI.Attack(event)
  -- event is unknown
  -- supposed to be Player
end

is this an intended feature or a bug?

#

im inclined to think this is a bug since when i define it inside the object

local a: AI = {
  Attack•
}

, typechecker says Attack is (_: Player) -> ()
but when filling it further

local a: AI = {
  Attack = function(event)
    event•
  end
}

, typechecker says event is unknown.

#

luau docs also don't say anything about that kind of behavior from foreign types

knotty wharf
#

giving a variable the same name as a type has no effect

#

they do not magically inherit

#

so what you actually have here is ```lua
local enemy: AI = {}
function someWhateverRandomUnrelatedTable.Attack(event)
-- event is unknown because this table is not enemy or anything related to the AI type
end

#

so no bug or feature, just user error

#

this is a bit more interesting however ```lua
--!strict
type AI = {
Attack: (Player) -> (),
}

local a: AI = {
Attack = function(event)

end;

}```

#

this seems to pick up the correct type but interesting that it is not inferred

narrow sand
#

that only seems to happen only for types that inherit from Instance, but Object, Color3, number work fine

knotty wharf
#

interesting that it can be inferred outside the declaration but not inside