ts-transformer-type-const
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

ts-transformer-type-const

A TypeScript custom transformer to inline literal types as JS runtime values

Requirement

TypeScript >= 3.2.0

How to use this package

This package exports 2 functions. One is constToValue which is used in TypeScript codes to obtain the value of given type, while the other is a TypeScript custom transformer which is used to compile the constToValue function correctly.

How to use constToValue

import { constToValue } from "ts-transformer-type-const";

type myType = "some value";
const myValue = constToValue<myType>();

console.log(myValue); // "some value"

How to use the custom transformer

Unfortunately, TypeScript itself does not currently provide any easy way to use custom transformers (See https://github.com/Microsoft/TypeScript/issues/14419).

ttypescript

See examples/ttypescript for detail. See ttypescript's README for how to use this with module bundlers such as webpack or Rollup.

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "plugins": [
      { "transform": "ts-transformer-type-const/transformer" }
    ]
  }
  // ...
}

Package Sidebar

Install

npm i ts-transformer-type-const

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

8.78 kB

Total Files

8

Last publish

Collaborators

  • davidmurdoch