@abstraqt-dev/nameof

This is a transformer for the TypeScript compiler that adds a whole new nameof keyword.

Usage no npm install needed!

<script type="module">
  import abstraqtDevNameof from 'https://cdn.skypack.dev/@abstraqt-dev/nameof';
</script>

README

This is a transformer for the TypeScript compiler that adds a whole new nameof keyword.

Example usage with WebPack and ts-loader:

In webpack.config.js:

  1. Import the nameof transformer
        const nameOfTransformer = require("./node_modules/@abstraqt-dev/nameof/lib/transformer").default;
  1. In the rules sction
        rules: [
        {
            test: /\.(ts|tsx)$/,
            loader: 'ts-loader',
            options: {
                getCustomTransformers: (program) => ({
                    before: [nameOfTransformer(program)],
                    after: []
                })
            },
            exclude: /node_modules/,
        },
  1. In tsconfig.json add the library to the included declarations files
    {
        ...,

        "types": [
            "@abstraqt-dev/nameof"
        ],
        
        ...
    }

In your code now you can just use the nameof keyword:

    interface IFoo {

    }

    class Bar {

    }

    let foo = nameof<IFoo>();
    let bar = nameof<Bar>();

    console.log(foo); // "IFoo"
    console.log(bar); // "Bar"