-
Notifications
You must be signed in to change notification settings - Fork 9
Type Resolver Transformer
The type resolver transformer is a program transformer that's built on top of the already existing macro transformer. It first transpiles the source code to typescript with all macros expanded, and then to javascript. This means that typescript is aware of all imports, type definitions, declarations, and exports that your macros expand to.
Here is an example of a macro that creates a type alias definition, we call the macro and use the type afterward:
Currently, you can only use this transformer with the ts-patch
library as it's the only one that provides program transformers.
To use the type resolver transformer with ts-patch, you have to specify it instead of the ts-macros plugin in your tsconfig.json
file:
"plugins": [
{ "transform": "ts-macros/dist/type-resolve", "transformProgram": true }
]
Now you won't get errors from tsc
or tspc
if you try to use types created by macros in your code, however, it's not that useful if your editor isn't aware of the created types.
If your editor doesn't support typescript syntax and intellisense, then this is enough to get the transformer running! Otherwise, head over to the next section!
- Patch the typescript server library with
ts-patch
:
ts-patch patch tsserver.js
ts-patch patch tsserverlibrary.js
- Change the version of typescript your editor is using to the patched version that's inside your
node_modules
- Make sure the
module
option in yourtsconfig.json
file is set tocommonjs
The transformer now gets executed inside your editor, so you'll be able to use everything generated by it.
If you decide to use the transformer in your editor, you will:
- Get intellisense on types, exports, and variable declarations generated by your macros. Generating imports works but as of right now, there's no way to let the editor know that they exist.
- Receive errors coming from ts-macros directly in your editor.
- Get macro chaining autocomplete.
- You will receive intellisense only on globally defined class, function, and variable declarations.
- Macro chaining autocomplete works only on macros' whose first parameter is a
string
,number
,Array
,boolean
, or a union of the aforementioned types.
This transformer is still in early development. It works reliably, but more testing is needed, and a lot more features will be polished and optimized in the future! If you encounter any errors or bugs, have suggestions regarding the transformer, or optimization ideas, feel free to create an issue!