get-type-text
An unconventional way of getting a type text at runtime in TypeScript
Why ?
- https://github.com/Microsoft/TypeScript/issues/14419
- I need to get a type text at runtime and I cannot hardcode it as string since it will get outdated on code refactors
- I don't want to use a tsc wrapper like ttypescript
What ?
npm install -D get-type-text
console.logx, z
Executing the program gives, as expected:
undefined undefined
But executing:
npx get-type-text
and running the program again we get:
Type<Date>
But at a terrible cost, go back to your source file and see how it changed:
console.logx, z
Options
Workflow
Basically use this only for test projects. Run npx get-type-text
before npm test
or tsc
, ts-node
, jest
, etc.
Rollback
To rollback the changes execute the following command. It will clean all the added arguments:
npx get-type-string --cleanArguments
API
(all the options apply to the CLI)
Motivation
Trying to test types utilities, like:
test'UnionOf transform a tuple into an union type',
Trying to develop a preprocessing tool to mutate TypeScript and replace certain function call expressions with referenced type text so we have access to this info at runtime. tsd-check is not enough for me since I need to verify types at runtime to reproduce false positives, and isNot helpers. (I cannot reproduce an error at compile time in a test)