As you can see, creating a Vector with a sigle number as argument, will create a Vector<3>. To create a scalar, you need to force it using an array
Array parameter
Vector can take in an array, and create a Vector with those values. The values will be assigned secuentially to the 1st, 2nd, 3rd, Nth dimension.
This array cannot be empty, and must be composed entirely by numbers
AlthoughVector supports taking in an Array as parameter, currently type inference doesn't work as expected, and falls back to the number type. See Types & Generics
A Vector can be creating from an existing one. Changes made to either won't be reflected on the other
import { Vector } from'volts';constfirstVec=newVector(1,2,3);constsecondVec=newVector(firstVec);firstVec.equals(secondVec); // truefirstVec.x =100;firstVec.equals(secondVec); // false// This is exactly the same as calling Vector.copy();// const secondVec = firstVec.copy();
Types & Generics
The Vector type is generic. This generic type describes the dimension of your vector, for example Vector<2> represents a two dimensional vector.
This generic type will determine which methods and properties are exposed to Typescript & Intelisense/autocompletion
You can hard-type the value, or you can leave it up to Typescript to figure out for you
import { Vector } from'volts';consttwoD=newVector(1,2); // Type get's infered to Vector<2>twoD.x; // ✅twoD.y; // ✅twoD.z; // ❌ highlighted as a type error ('z' doesn't exist on Vector<2>)
Hard-typing the generic is useful when dealing with cases when inference is not possible, for example, when constructing a Vector from an array.
import { Vector } from'volts';constarr= [1,2];constnoType=newVector(arr); // Type get's infered to Vector<number>// Vector<number> doesn't provide any useful type information, so instead 👇consthardTyped=new Vector<2>(arr) // Vector<2>// Note hard-typing to the wrong type will highlight as an error// Here, there's a collision between Vector<3> and Vector<2>constbadHardTyping=new Vector<3>(1,2); // ❌ highlighted as a type error
Vector<number> is the same as the combination of the Vector 1,2,3 & 4 types + base. Meaning you won't get any useful information based on the dimension of your Vector