javascript nameof

Solutions on MaxInterview for javascript nameof by the best coders in the world

showing results for - "javascript nameof"
Mirko
12 Jun 2017
1// TypeScript
2// NameOf.ts
3export const NameOf = <T>(name: keyof T) => name;
4
5// Setup
6class User {
7  Name: string;
8  Age: number;
9}
10
11// Usage
12import {NameOf} from './NameOf';
13function UseNameOf() {
14  console.log(NameOf<User>('Name'));
15}
16
17// The benefit is that, event though it's still technically
18// a string, TypeScript provides code completion when writing
19// that string. I.e., when you start typing a property of User,
20// is suggests 'Name' and 'Age'.