Hookyns / tst-reflect

Advanced TypeScript runtime reflection system
MIT License
337 stars 11 forks source link

[Question] getType returns either Invalid (using getType<Type>()) or unkonwn (using getType(var)) #93

Open zerobytes opened 1 day ago

zerobytes commented 1 day ago

I recently installed rttist in my typescript library. It's a very simple model library, where everything is just typescript classes and enums.

the generated metadata looks good.

the library is installed in a react application, where rttist is also installed

In the react app, I am calling getType, that is exported from the library through the metadata.typelib located in the library Library index.ts

export * from './metadata.typelib'

React App

import { getType, Metadata } from 'library-root-name'

The issue is, when I log getType it is either "Invalid Invalid [::invalid::Invalid]" or "Unknown unknown [#unknown]".

console.log(getType<ClassFromLibrary>().displayName) // Outputs Invalid Invalid [::invalid::Invalid]
const instance = new ClassFromLibrary();
console.log(getType(instance ).displayName) // Outputs Unknown unknown [#unknown]

For reference, ids are respectively "::invalid::Invalid" and "#unknown"


If I log Metadata.getModules(), all the library modules are correctly there. If I log Metadata.getTypes(), all the library types are correctly there.


1- I ran rttist init in the library 2- I ran rttist generate in the library 3- I moved the metadata.typelib to src 4- I exported all exports from metadata.typelib in my index.ts 5- I installed rttist in the react app 6- I imported the getType and Metadata that are being exported from the library 7- getType does not work as expected

Hookyns commented 1 day ago

You can use getType() function only when you setup transformer.

ad3) It should not be required. It should be generated into the project root folder; based on tsconfig. ad 4) Generated metadata.typelib.ts file is for the library project itself. Do not export it. ad 6) You have to generate metadata also in the React app and use it's metadata.typelib.ts. When you generate the metadata in the react project, there should be one extra import here: image There should be import of the library's public.typelib.js file from dist folder (or folder you configured). Check out this page in docs.

EDIT: But the main problem, in my opinion, is with the transformer. The rttist init does not configure transformers. The way you made the metadata work is clever, and it seems to work that way, but it's not how it should be done.