4 ms·
It would be great to be able to have access to types at runtime in order to change behavior based on types. That would make TypeScript so much more useful but i
by jupp0r 4y ago
It would be great to be able to have access to types at runtime in order to change behavior based on types. That would make TypeScript so much more useful but it does require javascript support.
- Matheus28 4y agoYou should try decorators for that. They're pretty decent
- jupp0r 4y agoYeah it's a workaround, but not really ergonomical for the library author (for example they can't modify the type being decorated).
- mixedCase 4y agoYou can use a runtime type system like io-ts or something like reflect-metadata. Let's not pay the cost when we don't need it.
- jupp0r 4y agoThe compiler knows when this is used and can only insert type metadata when needed. io-ts and reflect-metadata are unwieldy crutches compared to a type system that's designed to influence runtime behavior (ie Rust, C++, etc).
- rektide 4y agohttps://github.com/tc39/proposal-decorator-metadata https://github.com/tc39/proposal-decorator-metadata is where we'd probably need to begin to store this information. It's still need a transpiler to generate the necessary decorators. Decorators are still in progress (but kind of seemingly shaping up these days).