js nameof code example

Example: javascript nameof

// TypeScript
// NameOf.ts
export const NameOf = <T>(name: keyof T) => name;

// Setup
class User {
  Name: string;
  Age: number;
}

// Usage
import {NameOf} from './NameOf';
function UseNameOf() {
  console.log(NameOf<User>('Name'));
}

// The benefit is that, event though it's still technically
// a string, TypeScript provides code completion when writing
// that string. I.e., when you start typing a property of User,
// is suggests 'Name' and 'Age'.