get-type-text
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

get-type-text

An unconventional way of getting a type text at runtime in TypeScript

Why ?

What ?

npm install -D get-type-text
import TypeText from 'get-type-text'
import { UnionOf } from '..'
const x = TypeText<UnionOf<[1, Date[]]>>()
const z = TypeText<UnionOf<[1, boolean | string]>>()
console.log(x, z)

Executing the program gives, as expected:

undefined undefined

But executing:

npx get-type-text

and running the program again we get:

Type<Date> { a: 'a' } { a: "a" }
UnionOf<[1, Date[]]> UnionOf<[1, boolean | string]>

But at a terrible cost, go back to your source file and see how it changed:

import TypeText from 'get-type-text'
import { UnionOf } from '..'
const x = TypeText<UnionOf<[1, Date[]]>>('UnionOf<[1, Date[]]>')
const z = TypeText<UnionOf<[1, boolean | string]>>('UnionOf<[1, boolean | string]>')
console.log(x, 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:

import TypeText from 'get-type-text'
import { UnionOf } from '..'
test('UnionOf transform a tuple into an union type', () => {
  expect(2).not.toMatchType(TypeText<UnionOf<[1, false]>>)
  expect(1).toMatchType(TypeText<UnionOf<[1, false]>>)
})

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)

TODO / ISSUES

Package Sidebar

Install

npm i get-type-text

Weekly Downloads

3

Version

0.0.5

License

MIT

Unpacked Size

15.5 kB

Total Files

18

Last publish

Collaborators

  • cancerberosgx