#TypeScript Template with automatic reloading

1 messages · Page 1 of 1 (latest)

torpid orbit
#

I've been looking into a system sorta like this but something where we could return a class like Entity. where calling anything on the entity that requires validation would automatically throw a typescript error unless you do Entity.isValid before it.

But what it would allow you to do is have full type safety when dealing with entity, and also other such components with isValid. I haven't been able to fully develop it yet but it would be cool for you to look into it.

#

Something like this

export class Entity {
    public isValid(): this is ValidEntity;
}

export class ValidEntity {
    private constructor();
    /**
    ...
     */
    readonly dimension: Dimension;
    /**
   ...
     */
    readonly fallDistance: number;
    /**
    ...
     */
    readonly id: string;
    ...
#

And ValidEntity is just Entity class renamed

noble seal
#

I see

#

I check if Entity is not undefined just to be sure

noble seal
#

UPDATE: Compiling and reloading is 10x faster now

noble seal
torpid orbit
#

This system pretty much works for above ^

#

but I want to make it work any time your waiting a tick

#

it would add more checks into your code, but would also make sure to dont have ANY errors when handling objects

noble seal
#

Maybe I can find a way to make it more strict. I'll look into it

#

It's a matter of messing around with tsconfig.json

noble seal
#

Didn't find anything useful

torpid orbit
# noble seal Didn't find anything useful

Yeah Im actually liking this new system. So what I did is I renamed Entity to ValidEntity

And created a new Entity class type. I've only put the properties on their that are accessible when Entity.isValid is false. Using this system ensures that you dont accidentally use a entity property when your not allowed to use it.

export class Entity {
    /**
     * @remarks
     * Unique identifier of the entity. This identifier is intended
     * to be consistent across loads of a world instance. No
     * meaning should be inferred from the value and structure of
     * this unique identifier - do not parse or interpret it. This
     * property is accessible even if {@link Entity.isValid} is
     * false.
     *
     */
    readonly id: string;
    
    /**
     * @remarks
     * Identifier of the type of the entity - for example,
     * 'minecraft:skeleton'. This property is accessible even if
     * {@link Entity.isValid} is false.
     *
     */
    readonly typeId: string;

    public isValid(): this is ValidEntity;
}``` 

Although one problem is with `await` functions. Because you could be awaiting long enough where the entity becomes invalid.
#

Im thinking of applying this to all systems that use isValid to ensure the saftey of my code

noble seal
#

I see

#

I think I actually got your point now