@fluffy-spoon/name-of
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

What is this?

@fluffy-spoon/name-of makes it possible to get the name of a property at runtime, even if that type does not exist at runtime. Think of it as the nameof keyword in C#, but for TypeScript.

Installing

npm install @fluffy-spoon/name-of

Usage

import { getPropertyName, getDeepPropertyName } from '@fluffy-spoon/name-of';

interface SomeType {
  foo: boolean;

  someNestedObject: {
      bar: string;
  }
}

console.log(getPropertyName<SomeType>(x => x.foo)); //prints "foo"
console.log(getPropertyName<SomeType>(x => x.someNestedObject)); //prints "someNestedObject"
console.log(getPropertyName<SomeType>(x => x.someNestedObject.bar)); //prints "bar"

console.log(getDeepPropertyName<SomeType>(x => x.foo)); //prints "foo"
console.log(getDeepPropertyName<SomeType>(x => x.someNestedObject)); //prints "someNestedObject"
console.log(getDeepPropertyName<SomeType>(x => x.someNestedObject.bar)); //prints "someNestedObject.bar"

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.4.0
    677
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.4.0
    677
  • 1.3.0
    0
  • 1.2.0
    0

Package Sidebar

Install

npm i @fluffy-spoon/name-of

Weekly Downloads

677

Version

1.4.0

License

ISC

Unpacked Size

9.74 kB

Total Files

9

Last publish

Collaborators

  • ffmathy