A Babel plugin (v4.2.1) that implements the `nameof` keyword for TypeScript, mimicking C# `nameof` to get the string name of a variable, property, type, or member. It transforms compile-time expressions into their string representations, enabling refactor-safe string usage. Stable, maintained, and designed for use with Babel's TypeScript support. Different from ts-nameof (TypeScript compiler transformer) as it integrates into Babel pipelines. Requires @types/ts-nameof for type declarations.
npm install babel-plugin-ts-nameofVerified import paths — ran on the pinned version, not inferred.
Installs the plugin, configures Babel and TypeScript, then demonstrates compile-time nameof expressions for class, type, property, and member access.
Use only in TypeScript source files processed by Babel with this plugin enabled.
Ensure @babel/preset-typescript or similar is configured.
Upgrade to v4 and use @types/ts-nameof.
Assign the result of nameof() to a variable first and use that variable.
Check the documentation for supported syntax. Unsupported patterns will produce a compile error.
Run npm install @types/ts-nameof --save-dev and add "types": ["ts-nameof"] to compilerOptions in tsconfig.json.
Ensure Babel is configured with babel-plugin-ts-nameof in your build pipeline and that you are running the code through Babel.
Use 'babel-plugin-ts-nameof' (full npm package name) in plugins array.