Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

345
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda