After TSC compiled the TS code to JS, the types are removed and not available anymore.
But, in principle, it should be possible to make the types available at run-time by using a webpack/typescript/Vite transformer.
Has anyone achieved this?
type Data = {
name: string;
age: number;
};
// How can we implement `getType()`?
console.log(getType<Data>());
// It should print:
// { name: 'string', age: 'number' }
At compile-type, the type of Data is known, so it should be possible (albeit not easy) to implement a transformer that makes the type available at run-time.
This would enable a mirad of uses cases, such as IO validation.
For my use case I don't need advanced types such as generics. I only need the types: object, array, string, number, and date.
Context: I'm building an RPC implementation (https://github.com/brillout/wildcard-api).
Edit: I'm not looking for something like zod where you define a schema with zod and the TypeScript types are then inferred. My users define their types with TypeScript directly and I need to know these types at run-time. So I do need a compile-time transformer.