Registry / type-stubs / babel-plugin-ts-nameof

babel-plugin-ts-nameof

JSON →
library4.2.1jsnpmunverified

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-nameof
INSTALL
IMPORT
SIG · BABEL-PLUGIN-TS-NA
B
babel-plugin-ts-nameof
type-stubsjavascriptv4.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

nameof
import 'babel-plugin-ts-nameof' // global nameof() available
const nameof = require('babel-plugin-ts-nameof')
This is a Babel plugin; you don't import it in user code. The `nameof` function is a compile-time global.
plugin config
plugins: ['babel-plugin-ts-nameof']
plugins: ['ts-nameof']
Plugin name must be 'babel-plugin-ts-nameof', not just 'ts-nameof'.
type declarations
import 'ts-nameof' // or in tsconfig types
import { nameof } from 'ts-nameof'
The '@types/ts-nameof' package should be installed; import the module for global augmentation.

Installs the plugin, configures Babel and TypeScript, then demonstrates compile-time nameof expressions for class, type, property, and member access.

// Install: npm install babel-plugin-ts-nameof @types/ts-nameof --save-dev // .babelrc { "plugins": ["babel-plugin-ts-nameof"] } // tsconfig.json (for type declarations) { "compilerOptions": { "types": ["ts-nameof"] } } // Usage class Person { firstName: string; lastName: string; } const name = 'firstName'; console.log(nameof(Person)); // "Person" console.log(nameof<Person>()); // "Person" console.log(nameof<Person>('firstName')); // "firstName" console.log(nameof<Person>(p => p.lastName)); // "lastName"
Debug
Known issues
gotchanameof() only works at compile time; it is not available in runtime JavaScript.
fix
Use only in TypeScript source files processed by Babel with this plugin enabled.
affects: >=1.0
gotchaThe plugin requires Babel's TypeScript preset or TypeScript compilation; does not work with plain JavaScript.
fix
Ensure @babel/preset-typescript or similar is configured.
affects: >=1.0
deprecatedPrevious versions (v3.x) used a different import style for type declarations.
fix
Upgrade to v4 and use @types/ts-nameof.
affects: >=3.0 <4.0
gotchanameof() cannot be used in evaluated code (e.g., inside template literals that are not strings).
fix
Assign the result of nameof() to a variable first and use that variable.
affects: >=1.0
gotchaDoes not support all TypeScript types; only named types, properties, and methods are supported.
fix
Check the documentation for supported syntax. Unsupported patterns will produce a compile error.
affects: >=1.0
Errors
Common errors & fixes
Cannot find name 'nameof'.
TypeScript cannot resolve the global `nameof` type because @types/ts-nameof is not installed or not included in tsconfig.
fix
Run npm install @types/ts-nameof --save-dev and add "types": ["ts-nameof"] to compilerOptions in tsconfig.json.
TypeError: nameof is not a function
Runtime error because the plugin is not applied. nameof is a compile-time construct, not a runtime function.
fix
Ensure Babel is configured with babel-plugin-ts-nameof in your build pipeline and that you are running the code through Babel.
Error: Plugin 'ts-nameof' not found
Incorrect plugin name in Babel configuration.
fix
Use 'babel-plugin-ts-nameof' (full npm package name) in plugins array.
Upgrade
Version history
4.2.1latest on npm
Audit
Dependencies
@types/ts-nameofoptionalProvides TypeScript type declarations for the `nameof` global function
Agent activity
52 hits · last 30 days
node
46
OpenAI (training)
1
Resources
babel-plugin-ts-nameof — npm install babel-plugin-ts-nameof · libregistry