-
Hi, I am new to koota but it looks interesting for one of my current projects (React + PixiJS + koota + state-machines). Am I wrong to think that Eg import { createWorld, trait } from "koota";
const world = createWorld();
const Car = trait();
const Color = trait<{ readonly color: string }>();
const car = world.spawn(Car, Color({ color: "red" }));
console.log("car has Car", car.has(Car));
console.log("car has Color", car.has(Color));
console.log("car Color", car.get(Color), car.get(Color)?.color); The output is car has Car true
car has Color true
car Color {} undefined I am expecting the last line of output to be Please help me understand what I'm doing wrong! Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Update: I found that providing a default value for the data trait fixes this const Color = trait<{ readonly color: string }>({ color: "yellow" }); Now in the output I do see car has Car true
car has Color true
car Color {
color: "red",
} red If it is indeed mandatory to have a default value for the trait perhaps the TypeScript types could be improved to flag this an an error? |
Beta Was this translation helpful? Give feedback.
Hi Maurice. Yes, a schema is required for the trait. I'll check to see why it doesn't give an error if you explicitly type the trait but don't pass in a matching schema. This should be fixable.
By the way, if no schema is passed in then the trait is considered a tag and holds no data. This is why you are seeing
undefined
.