typeof(vector) returning incorrect value

local v = vector.create(1, 2)

print(type(v)) -- vector
print(typeof(v)) -- Vector3

print(type(buffer)) -- buffer
print(typeof(buffer)) -- buffer

print(type(0)) -- number
print(typeof(0)) -- number

print(type("")) -- string
print(typeof("")) -- string

typeof returning Vector3 feels like a bug to me

there is no way to differentiate between a Vector3 and vector when using just typeof

Expected behaviour

I feel that the correct value should be vector for both type and typeof

2 Likes

Under the hood in Luau, vector and Vector3 are one and the same. This won’t ever be changed due to backwards compatibility necessities. Would you want all code expecting typeof(vec3) == "Vector3" to start failing?

type will always give you the primitive name of the type, while typeof gives you the decorated name of the type. Passing a Vector3 into type will give you `vector’.

I don’t think this entirely makes sense here because the vector library is very new to rblx luau, so most things built this way shouldn’t accept vector objects anyways if they expect a Vector3.
Because of this, your example of typeof(vec3) would only actually return something different if using this new type that should already be incompatible with old code.

Not sure if I worded that right, but I agree with this request and it would be nice as the vector library is still new and this shouldn’t be a breaking change

if both types behaved the same then maybe it would not be to much of a big deal but

for example if I do

if typeof(v) == "Vector3" then
    local sign = v:Sign()
end

will error if v is a vector

I would be forced to do

local theRealType = if type(v) == "vector" then "vector" else typeof(v)
if theRealType == "Vector3" then
    local sign = v:Sign()
end
1 Like

That’s definitely a new one. I do agree there needs to be cross compatibility between the two at all times, but do keep in mind there’s also a vector.sign function that should consume both.

Right now the type annotations are busted and that needs to be fixed on their end, but it otherwise should work.

I was not aware that vector.sign could consume both a vector and a Vector3

There’s also the issue of some things returning a Vector3 when inputting a vector or visa versa. For example setting an attribute using a vector returns a Vector3 when getting that same attribute.

That is beyond just the type check though, it’s the object itself.

Again, type(Vector3.zero) == "vector" evaluates to true. You can confirm this to be the case.
The following primitive types exist in Luau:

  • nil
  • table
  • string
  • vector
  • buffer
  • thread
  • boolean
  • number
  • function
  • userdata

typeof(someValue) returns:

  • "Vector3" for any vector (for backwards compatibility)
  • The value of a userdata’s __type field (only when set in C++)
  • Otherwise type(someValue).
1 Like

See my above post. Vector3 is just an alias to vector.

The Vector3 and vector classes are a lot more interchangeable than I remember them being. Seems like you can throw either into the vector library, use the methods, and get properties included in either.

I remember it erroring when you tried to do those things but they work together fine so I suppose it doesn’t matter which is which, just how you use it.

They definitely made some last minute changes because I remember vectors being separate enough that they would error where they don’t anymore… It’s definitely a good thing Vector3s work with the vector library now though
Ex: vector.magnitude() is significantly faster than Vector3.Magnitude (about 2-3x boost), and similar with other functions. This is an important thing for some performance-intensive tasks

The reported behavior is intentional.

typeof returns Vector3 to preserve backwards compatibility.
type was changed because it wasn’t very useful in the past when it returned ‘userdata’, but we still had an announcement for that change: Behavior change: type() will start returning 'vector' for Vector3

1 Like