Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
How to get type key name from object key in typescript?
type transaction = {
    uid: string,
    paymentMode : string,
}

I want to get key name from type for e.g.

function getFieldName(input) {
  returns a string with key name
}

const tran : transaction = {}
getKeyName(tran.paymentMode) returns 'paymentMode'

I read many articles and tried some solutions, so I understand we will at least need to create object for that type which is fine.

I want to do this because key name of a type is required and I also want to keep auto-complete for key name when we type something like obj.key to avoid mistakes.

I know, I can create a separate object from type with constant key, value pair. I want to avoid this, because then we will need to change same thing at 2 places whenever there is a change in type, which can be missed.

Edit:

I was looking for exactly this - nameof in c#. @Aleksey L. told this in comments.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This seems to be the type of function signature that you want:

Note that it meets your criteria of compatibility with IntelliSense auto-complete/suggest:

enter image description here

TS Playground

type Transaction = {
  uid: string;
  paymentMode: string;
};

function getFieldName <T extends object, K extends keyof T>(o: T, key: K): K {
  return key;
}

const transaction: Transaction = {
  uid: window.crypto.randomUUID(),
  paymentMode: 'unknown',
};

getFieldName(transaction, 'uid'); // "uid"
getFieldName(transaction, 'paymentMode'); // "paymentMode"

getFieldName(transaction, 'asdf'); /*
                          ~~~~~~
Argument of type '"asdf"' is not assignable to parameter of type 'keyof Transaction'.(2345) */


It is not possible to derive a string property name in JavaScript (or TypeScript) by passing a reference to the property value. JavaScript simply doesn't have this kind of meta-introspection capability at present.

It appears that your question is not just about type safety, but about DX/ergonomics. The above solution does require providing the property name, but the number of extra characters to type before getting the IntelliSense prompt is only 1 (2 if you consider whitespace):

// For: obj.prop:

// desired:
fn(obj.p_)
//   012^

// proposed:
fn(obj, 'p_')
//   01234^
// The closing quote is auto-inserted by IntelliSense, just like the closing parenthesis.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!