1.0.0 β€’ Published 10 months ago

@nameof/nameof v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

nameof

A tiny utility for safely extracting property names from TypeScript objects β€” inspired by C#'s nameof operator.

πŸ’‘ Examples

import nameof from "@nameof/nameof";

// basic
const obj = { name: "John", age: 30 };
const keyName = nameof(obj, "name");
console.log(`keyName: ${keyName}`);

// single key
const keyName2 = nameof<typeof obj>("age");
console.log(`keyName2: ${keyName2}`);

// variable name
const numbers = [1, 2, 3, 4, 5];
const variableName = nameof({ numbers });
console.log(`variableName: ${variableName}`);

// Class Name
class Person {}
const className = nameof(Person);
console.log(`className: ${className}`);

// Function Name
function bar() {}
const functionName = nameof(bar);
console.log(`functionName: ${functionName}`);

Try Online (only type)

✨ Features

  • βœ… Type-safe access to property names
  • βœ… Preserves literal types
  • βœ… Refactor-friendly
  • βœ… Zero dependencies

πŸ“¦ Installation

# With Bun
bun add @nameof/nameof

with npm yarn pnpm amd more see here

πŸš€ Usage

import nameof from "@nameof/nameof";

const user = {
  id: 1,
  name: "Alice",
  age: 30,
};

const propName = nameof(user, "name"); // "name"

🧠 Why?

TypeScript lacks a built-in nameof operator like C#. Developers often fall back on hardcoded strings, which:

  • ❌ Aren't checked by the compiler
  • ❌ Don’t update when properties are renamed
  • ❌ Easily lead to bugs in refactors

This utility gives you type-safe property name extraction, with full support for TypeScript’s type system and IDE refactor tools.

πŸ›‘οΈ Refactor-Safe by Design

One of the key benefits of nameof is its compile-time safety during refactoring.

πŸ” Rename-safe

const user = {
  id: 123,
  name: "Alice",
};

const field = nameof(user, "name"); // "name"

Now, if you rename name to fullName:

const user = {
  id: 123,
  fullName: "Alice",
};

// ❌ TypeScript error:
const field = nameof(user, "name");
// Argument of type '"name"' is not assignable to ...

βœ… TypeScript catches it instantly. Your code is safe from silent bugs caused by outdated strings.

πŸ” What about hardcoded strings?

const field = "name"; // ❌ No error on rename

Hardcoded strings aren’t tracked by the compiler β€” you lose all refactor safety.

πŸ’‘ Pro Tip: Use as const

For stricter typing:

const config = {
  apiEndpoint: "/api/data",
  retry: true,
} as const;

const key = nameof(config, "apiEndpoint"); // type: "apiEndpoint"

Literal types are preserved in the result!

❌ Why Not a Macro or Compiler?

Some libraries implement nameof(foo.bar) using Babel or AST transforms. This package deliberately avoids those approaches.

🚫 No macros or compilers required

  • ❌ No Babel plugins
  • ❌ No SWC or TypeScript transformers
  • ❌ No custom build setup

βœ… Just TypeScript

  • Works in any environment (Node, Bun, Vite, Webpack, etc.)
  • Transparent and reliable
  • Keeps your build chain simple

πŸ”— References

πŸ”§ Development

This project is built with Bun β€” fast, modern, and TypeScript-first.

git clone https://github.com/baboon-king/nameof.git
cd nameof
bun install
bun run build

πŸ“„ License

MIT License Β© 2025-PRESENT BaboonKing

🌸 Thanks

This project is heavily inspired by the following awesome projects.

πŸ™Œ Contributing

Suggestions, issues, and PRs are welcome!

If you find this useful, please ⭐️ the repo and help others discover it!

1.0.0

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago