#Get module from prototype
74 messages ยท Page 1 of 1 (latest)
no such data is maintained, decorators might let you do that though
bruh why is there so little reflection in ts
why would you need reflection though
why not
also there is actually a lot of reflection
for a lot of stuff
thats not a ton but its nice yeah
that is a ton
but it would be nice if ts retained declared types and modules
like compared to c# what is it missing
types
typescripts goal is to not exist at runtime
types dont exist in js
prototypes are objects, classes are classes
func(MyClass) passes the prototype of instances of MyClass to the function
if im not stupid
even better then
also have the class
so theres enoguh type related stuff to have type retention as metadata
theoretically yes
realistically it's difficult to figure out a nice way to do this
ig
it's easy enough to force yourself to annotate the correct types manually though
(as in, emit an error if they're missing)
well idk actually
idk if esquery is powerful enough
for this specifically tho i want to get the module of any class passed
so i cant annotate every class
becasuse some arent from my code
what do u need the module for anyway
but why do you need to do that
idk it seems nice
if you can compare the class objects directly
nah im getting the name as a string
but prefereably id want to prefix the name with the module
but why o.o
for uniqueness in the map
if i have @discordjs.Client and @someothermodule.Client i dont want them to conflict
also doesnt Node print the declaring module name of a class in errors and stuff?
this might seem a bit weird but you could use the class objects themselves as keys in your Map:
Preview:```ts
type Constructor = new (...args: never[]) => unknown
class Foo {
isFoo = true
}
class Bar {
isBar = true
}
namespace SomeOtherModule {
export class Foo {
isADifferentFoo = true
}
}
const x = new Map<Constructor, {}>([
[Foo, new Foo()],
[Bar, new Bar()],
...```
yeah true but i want them to optionally be accessible by a specific name too
if you have multiple instances
idk
this works ig
another option is to give every class that wants to participate in this scheme a name: string property and write whatever you want there. it won't be as automagic as reflection but you can include a prefix to identify the module or whatever yourself
yeah id probably do smth like that with decorators if i could but i want to do this for any class
from other modules too
gotcha
Map<any class, string> for names, lol
its easy enough to get module path tbh
put a new method on object that throws an exception
class.method()
or not, since the class code wouldn't be in the stack trace
you could use something from import.meta i think
assuming ESM
but anything like this would still require modifying the class definition, which it sounds like orbyfied doesn't want
oh, unless this meant monkeypatching the class. that's clever, but yeah i don't think it'd help with this
nah when i said object i meant Object. but yeah unfortunately it doesnt show lines that don't execute ๐
i got it. use Class.toString() as the keys. classes with exactly the same source code will collide, but, i mean, structural typing and all ๐