#Static issue with user-defined type guard
1 messages · Page 1 of 1 (latest)
this doesn't work on types, only in interfaces
Oh, right
and functions have a this parameter, not a this type
yep 👍
because how this works, is that it's whatever the subclass is
However the this is T idea would still work appropriately, right?
i guess the idea is that usually for types you dont really care about inheritance
if you convert it to an interface it should work fine
awesome, thanks a lot for the help :)
you could try manually adding a this parameter, which might make typescript recognize that its a parameter name not the special this type?
i wouldn't know, i've never tried
right, yeah I was just about to ask that.
!pg
I'm wondering what happens if I mix and match types and interfaces. I'll have a play around :)
again- thanks a lot for the help
!resolved
export type Entity = {
hasComponent<T>(this: Entity, componentName: string): this is T;
// ^^^^
// A 'this' type is available only in a non-static member of a class or interface.
}
yea
I come from a lua background, so where I come from we don't really worry about this or self, everything is just a static function :p
In a type you mean?
function obj:method () end
i do the very idiosyncratic local f = function () end though
hah, yeah classic
in an effort to minimize the number of syntax constructs im using
not like its harder to maintain or anything so might as well, right?
and not like it's different semantically unlikecough JS
haha yeah sure, each to their own! :)
My past codebase had this kindof thing all over the place
in hindsight it hurts readibility a tiny bit, but like, does it really matter that much?