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

144
Vistas
TypeScript: How to create union type from array property on mapped type?

I am trying to create a union type from the elements of an array in an object that has been annotated to a mapped type. Is it possible to achieve somehow without removing the type annotation?

Example code:

type Persons<Name extends string> = {
    readonly [N in Name]: {
        readonly age: number;
        readonly friends: ReadonlyArray<Name>;
    };
}

type Friends<Name extends string, P extends Persons<Name>, N extends Name> = P[N]["friends"][number]

type Names = 'Bill' | 'Jim' | 'Tom'

// Example with type annotation that I would like to keep
const persons1: Persons<Names> = {
    Bill: {
        age: 40,
        friends: ['Jim']
    },
    Jim: {
        age: 42,
        friends: ['Tom']
    },
    Tom: {
        age: 45,
        friends: []
    }
}

// Example without type annotation (for which it works as expected)
const persons2 = {
    Bill: {
        age: 40,
        friends: ['Jim']
    },
    Jim: {
        age: 42,
        friends: ['Tom']
    },
    Tom: {
        age: 45,
        friends: []
    }
} as const

type Friends1 = Friends<Names, typeof persons1, 'Jim'>
// 'Jim' | 'Bill' | 'Tom' - would like it to be 'Tom'

type Friends2 = Friends<Names, typeof persons2, 'Jim'>
// 'Tom' - as wanted

My guess was that it could be done by deep cloning persons1 but haven't been able to get the correct type of the clone.

about 4 years ago · Santiago Gelvez
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