https://github.com/ffmathy/fluffyspoon.javascript.nameof
https://github.com/ffmathy/fluffyspoon.javascript.nameof
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ffmathy/fluffyspoon.javascript.nameof
- Owner: ffMathy
- License: mit
- Created: 2020-09-25T13:03:23.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-25T19:28:04.000Z (almost 6 years ago)
- Last Synced: 2025-04-11T05:06:03.593Z (about 1 year ago)
- Language: TypeScript
- Size: 59.6 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# What is this?
[`@fluffy-spoon/name-of`](https://www.npmjs.com/package/@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
```typescript
import { getPropertyName, getDeepPropertyName } from '@fluffy-spoon/name-of';
interface SomeType {
foo: boolean;
someNestedObject: {
bar: string;
}
}
console.log(getPropertyName(x => x.foo)); //prints "foo"
console.log(getPropertyName(x => x.someNestedObject)); //prints "someNestedObject"
console.log(getPropertyName(x => x.someNestedObject.bar)); //prints "bar"
console.log(getDeepPropertyName(x => x.foo)); //prints "foo"
console.log(getDeepPropertyName(x => x.someNestedObject)); //prints "someNestedObject"
console.log(getDeepPropertyName(x => x.someNestedObject.bar)); //prints "someNestedObject.bar"
```