{"id":19230,"library":"babel-plugin-ts-nameof","title":"babel-plugin-ts-nameof","description":"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.","status":"active","version":"4.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/dsherret/ts-nameof","tags":["javascript","nameof","typescript","transforms","babel"],"install":[{"cmd":"npm install babel-plugin-ts-nameof","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-ts-nameof","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-ts-nameof","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides TypeScript type declarations for the `nameof` global function","package":"@types/ts-nameof","optional":true}],"imports":[{"note":"This is a Babel plugin; you don't import it in user code. The `nameof` function is a compile-time global.","wrong":"const nameof = require('babel-plugin-ts-nameof')","symbol":"nameof","correct":"import 'babel-plugin-ts-nameof' // global nameof() available"},{"note":"Plugin name must be 'babel-plugin-ts-nameof', not just 'ts-nameof'.","wrong":"plugins: ['ts-nameof']","symbol":"plugin config","correct":"plugins: ['babel-plugin-ts-nameof']"},{"note":"The '@types/ts-nameof' package should be installed; import the module for global augmentation.","wrong":"import { nameof } from 'ts-nameof'","symbol":"type declarations","correct":"import 'ts-nameof' // or in tsconfig types"}],"quickstart":{"code":"// Install: npm install babel-plugin-ts-nameof @types/ts-nameof --save-dev\n// .babelrc\n{\n  \"plugins\": [\"babel-plugin-ts-nameof\"]\n}\n// tsconfig.json (for type declarations)\n{\n  \"compilerOptions\": {\n    \"types\": [\"ts-nameof\"]\n  }\n}\n// Usage\nclass Person {\n  firstName: string;\n  lastName: string;\n}\nconst name = 'firstName';\nconsole.log(nameof(Person)); // \"Person\"\nconsole.log(nameof<Person>()); // \"Person\"\nconsole.log(nameof<Person>('firstName')); // \"firstName\"\nconsole.log(nameof<Person>(p => p.lastName)); // \"lastName\"","lang":"typescript","description":"Installs the plugin, configures Babel and TypeScript, then demonstrates compile-time nameof expressions for class, type, property, and member access."},"warnings":[{"fix":"Use only in TypeScript source files processed by Babel with this plugin enabled.","message":"nameof() only works at compile time; it is not available in runtime JavaScript.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Ensure @babel/preset-typescript or similar is configured.","message":"The plugin requires Babel's TypeScript preset or TypeScript compilation; does not work with plain JavaScript.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Upgrade to v4 and use @types/ts-nameof.","message":"Previous versions (v3.x) used a different import style for type declarations.","severity":"deprecated","affected_versions":">=3.0 <4.0"},{"fix":"Assign the result of nameof() to a variable first and use that variable.","message":"nameof() cannot be used in evaluated code (e.g., inside template literals that are not strings).","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Check the documentation for supported syntax. Unsupported patterns will produce a compile error.","message":"Does not support all TypeScript types; only named types, properties, and methods are supported.","severity":"gotcha","affected_versions":">=1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run npm install @types/ts-nameof --save-dev and add \"types\": [\"ts-nameof\"] to compilerOptions in tsconfig.json.","cause":"TypeScript cannot resolve the global `nameof` type because @types/ts-nameof is not installed or not included in tsconfig.","error":"Cannot find name 'nameof'."},{"fix":"Ensure Babel is configured with babel-plugin-ts-nameof in your build pipeline and that you are running the code through Babel.","cause":"Runtime error because the plugin is not applied. nameof is a compile-time construct, not a runtime function.","error":"TypeError: nameof is not a function"},{"fix":"Use 'babel-plugin-ts-nameof' (full npm package name) in plugins array.","cause":"Incorrect plugin name in Babel configuration.","error":"Error: Plugin 'ts-nameof' not found"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}