3 ms·
Does this mean Node can know if an exception is subclass of ValueError or an object is instance of SomeClass? I'm a TS newb, I thought types outside of array, o
by aitchnyu 2y ago
Does this mean Node can know if an exception is subclass of ValueError or an object is instance of SomeClass? I'm a TS newb, I thought types outside of array, object, number, string arent present in JS and Zod and typeguard functions return plain objects with "trust me bro".
- LelouBil 2y agoYou are right, they aren't. In the JavaScript languages which is what gets actually executed, there are no typescript types. The parent commenter was talking about a way for nodejs to provide, via an API, the content of type annotations on fields/functions/variables like in python. However, in python the type annotations are a property of the object at run time, whereas they are completely stripped before execution for typescript. So I'm not sure how it would work except by changing the typescript philosophy of "not changing runtime execution"
- gampleman 2y agoIn JS, classes do retain runtime information. So the `instanceof` is a real runtime operator that works by checking the prototype chain of an object. So checking subclasses can be done at runtime. However, in TS other type information is erased at compile time. So if you write type Foo = "a" | "b"; the runtime code will see that just as a plain string.