8000 Recommend: Don't use this package · Issue #121 · dsherret/ts-nameof · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Recommend: Don't use this package #121
Open
@dsherret

Description

@dsherret

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0