question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recommend: Don't use this package

See original GitHub issue

I now recommend not using this package or any other compiler transforms. It’s neat, but it creates code that is not portable and makes it hard to switch to new build systems. The current solutions for injecting compiler transforms are hacky and I can’t imagine the TS compiler ever supporting this out of the box.

I personally now just use the following function. It’s simple and it works for most cases… of course it’s not as featureful, but it gets you 90% of the way there.

export function nameof<TObject>(obj: TObject, key: keyof TObject): string;
export function nameof<TObject>(key: keyof TObject): string;
export function nameof(key1: any, key2?: any): any {
  return key2 ?? key1;
}

Example Use

import { nameof } from "./some-relative-import";

interface SomeInterface {
  someProperty: number;
}

// with types
console.log(nameof<SomeInterface>("someProperty")); // "someProperty"

// with values
const myVar: SomeInterface = { someProperty: 5 };
console.log(nameof(myVar, "someProperty")); // "someProperty"

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:26
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
sotnikov-linkcommented, Jan 14, 2022

I published package ts-keyof. Maybe it will be useful for someone. Thanks for ts-nameof! I used it about 3 years, but now I need use esbuild and swc.

2reactions
dobromyslovcommented, Dec 23, 2021

Thank you for your candor. It would be awesome if you also write a note to this answer https://stackoverflow.com/a/33556815/1177597 when you have spare time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

apt - How to not install recommended and suggested packages?
Use apt-mark hold package-that-apt-should-leave-alone . hold hold is used to mark a package as held back, which will prevent the package from being ......
Read more >
Best practices for writing Dockerfiles - Docker Documentation
This document covers recommended best practices and methods for building ... Use this syntax to build an image using a Dockerfile from stdin...
Read more >
Package Delivery Boxes to Deter Porch Pirates
At-Home Review: Package Delivery Boxes to Deter Porch Pirates. These dedicated drop boxes could prevent package theft—if delivery drivers were ...
Read more >
When not to use package-lock.json - DEV Community ‍ ‍
I am not advocating against using package-lock.json for programs designed to be consumed by the end consumer. I do suggest not to use...
Read more >
Publishing and installing a package with GitHub Actions
If you want your workflow to access a GitHub Packages registry that does not support granular permissions, then we recommend using the GITHUB_TOKEN...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found