rbxts-flamework / core

Flamework is an extensible game framework. It requires typescript and offers many useful features.
MIT License
101 stars 8 forks source link

add a second generic to BaseComponent for the instance of the component #2

Closed Xuleos closed 3 years ago

Xuleos commented 3 years ago

the current solution for guarding the type of a component looks a little akward:

import { Component, BaseComponent, OnStart, Flamework } from "@rbxts/flamework";

interface Attributes {}

const guard = Flamework.createGuard<Model>();

@Component({
    tag: "Structure",
})
export class Structure extends BaseComponent<Attributes> implements OnStart {
    structure!: Model

    onStart() {
        if (!guard(this.instance)) {
            error("no")
        }

        this.structure = this.instance
    }
}

It would be much simpler for users if you added a second generic to BaseComponent and then did your voodoo ts plugin shit to automatically add a type guard to the component

import { Component, BaseComponent, OnStart, Flamework } from "@rbxts/flamework";

interface Attributes {}

@Component({
    tag: "Structure",
})
export class Structure extends BaseComponent<Attributes, Model> implements OnStart {
    onStart() {
        print(this.instance.PrimaryPart) //wooo this is legal because this.instance is a model
    }
}
FutrScance commented 3 years ago

B)