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

246
Vistas
Can I define a Typescript interface with computed keys?

I want to define a Typescript interface with computed keys:

const NAMESPACE = 'com.pizza'

const KEY_SAUCE = `${NAMESPACE}/sauce`
const KEY_CRUST = `${NAMESPACE}/crust`

interface PizzaToken {
  radius:      number
  [KEY_SAUCE]: Sauce
  [KEY_CRUST]: Crust
}

const pizza: PizzaToken = PizzaFactory.getToken(...)
console.log(pizza[KEY_SAUCE])

This design pattern gives me two unrelated Typescript errors; the first, on the interface:

TS1169: A computed property name in an interface must refer to an 
expression whose type is a literal type or a 'unique symbol' type.

and the second on the pizza[KEY_SAUCE]:

TS7053: Element implicitly has an 'any' type because expression of 
type 'any' can't be used to index type 'Pizza'.

I can resolve these with some workarounds (e.g Symbol) but they're kind of clunky. What's the best way to make something like this work?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to use as const to make a nonwidening string literal type at the key declarations:

const KEY_SAUCE = `${NAMESPACE}/sauce` as const;
const KEY_CRUST = `${NAMESPACE}/crust` as const;
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