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

176
Views
¿Hay alguna manera de mantener el valor al que se refiere un objeto como una clave de referencia de cadena y no como el valor real?

Dado este código:

 const colors = { WHITE: "#FFF", }; const style = { backgroundColor: colors.WHITE, };

A lo largo de mi código, cuando accedo a style.backgroundColor , quiero que el valor sea "#FFF" como se esperaba. Pero en un punto específico me gustaría conocer la ruta de la clave de referencia como una cadena. Por ejemplo, quiero que el valor sea "colors.WHITE" . ¿Hay una manera de hacer eso?

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

0

Entiendo que puede ser demasiado, pero puedes probar esto:

 type ComputeRange< N extends number, Result extends Array<unknown> = [], > = (Result['length'] extends N ? Result[number] : ComputeRange<N, [...Result, Result['length']]> ) type HexNumber = `${ComputeRange<10>}` type HexString = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' type Hex = `${HexNumber}` | HexString; type StringLength< Str extends string, Acc extends string[] = [] > = (Str extends `${infer S}${infer Rest}` ? StringLength<Rest, [...Acc, S]> : Acc['length']) type ValidateLength< Str extends string, Length extends number > = (StringLength<Str> extends Length ? Str : never) type ValidateHex< Color extends string, Cache extends string = '', > = Color extends `${infer A}${infer Rest}` ? (A extends '' ? Cache : (A extends Hex ? ValidateHex<Rest, `${Cache}${A}`> : never) ) : Cache type ExtractHash<T extends string> = T extends `#${infer Rest}` ? Rest : never type ValidateMap<T extends Record<string, string>> = { [Prop in keyof T]: T[Prop] & ValidateHex<T[Prop]> & ValidateLength<T[Prop], 6 | 3> } type AddHash<T extends Record<string, string>> = { [Prop in keyof T]: `#${T[Prop]}` } const color = < Key extends string, Value extends string, ColorMap extends Record<Key, Value> >(dictionary: AddHash<ValidateMap<ColorMap>>) => dictionary const colorTheme = color({ white: '#ffffff', gray: '#e2e2e2', green: '#ffx' // expected error }) colorTheme.gray // '#e2e2e2'

Patio de juegos

Ventajas:

TS validará su valor hexadecimal

Contras:

Debe crear una función de tiempo de ejecución ficticia para la validación de inferencias.

Si está interesado en esta solución, le daré alguna explicación.

Mientras tanto, puede consultar mi artículo y la respuesta relacionada.

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!