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

296
Views
¿Cómo hacer cumplir el tipo consistente dentro de los objetos (Mecanografiado)?

He llegado hasta aquí: lo que parece funcionar

 function test<types extends Record<string,any>>(dict: dictionary<types>){} type dictionary<types extends Record<string, any>> = { [key in keyof types]: { bar?: types[key]; foo?: (value:types[key])=>true; }; }; test({ key1:{ bar: 2, foo: (input:number)=>true, }, key2:{ bar: 'hello' foo: (input: number)=>true, // Error! "input" needs to be string } })

¡PERO! También necesito una referencia de tipo genérico al parámetro dict . Y por alguna razón, esto no funciona.

 function test2< types extends Record<string,any>, dictionary extends dictionary2<types> // <-- Added a generic type >(dict: dictionary){} // Same as above type dictionary2<types extends Record<string, any>> = { [key in keyof types]: { bar?: types[key]; foo?: (value:types[key])=>true; }; }; // Same as above test2({ key1:{ bar: 2, foo: (input: number)=>true, }, key2:{ bar: 'hello', foo: (input:number)=>true,// Should be an Error (but isn't)! "input" needs to be string }

Enlace del patio de recreo

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

0

Podrías hacer esto:

 function test2<T extends Record<string, unknown>>(dict: Dictionary<T>) { } type Dictionary<T> = { [key in keyof T]: { bar?: T[key]; foo?: (value: T[key]) => true; }; } // Same as above test2({ key1: { bar: 2, foo: (input: number) => true, }, key2: { bar: 'hello', foo: (input: number) => true, // Actual error } });

patio de recreo mecanografiado

about 4 years ago · Juan Pablo Isaza Report

0

Cambie el alcance de la inferencia para que se infieran los types y se escriba dict en función de esa inferencia en lugar de un segundo parámetro de tipo, es decir,

 function test2< types extends Record<string,any> >(dict: dictionary2<types>){}

Zona de juegos de trabajo aquí .

EDITAR: Ejemplo de uso con mayúsculas convencionales

 function test2< T, >(dict: Dictionary<T>){ type FullDictionary = Dictionary<T> // can't you just use this in your function? } type Dictionary<T extends Record<string, any>> = { [K in keyof T]: DictionaryEntry<T[K]> } type DictionaryEntry<T> = { bar?: T foo?: (value:T)=>true } test2({ key1:{ bar: 2, foo: (input: number)=>true, }, key2:{ bar: 'hello', foo: (input:number)=>true } })
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!