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

152
Views
TypeScript: infiere el tipo de <this> en el método de extensión de objeto

Estoy tratando de implementar algo similar a la función let scope de kotlin en TypeScript.

Mi enfoque actual es usar la fusión de declaraciones con la interfaz de Object . Lo que generalmente funciona, pero me falta la información de tipo del argumento para el método interno (vea el ejemplo a continuación).

¿Hay alguna manera de inferir el tipo de Objeto donde se invocó la función?

 interface Object { let: <S>(block: <T = this>(thisVal: T) => S) => S | null } Object.prototype.let = function <T, S>(block: (thisVal: T) => S): S | null { return this != null ? block(this as T) : null } const a = {foo: 42} // don't want to write this -> 'a.let<typeof a>(it => console.log(it.foo));' a.let(it => console.log(it.foo)); // type of 'it' is T = Object

Prueba aquí en TS Playground

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede hacer esto agregando un parámetro this a la función let y capturar this desde la llamada let :

 interface Object { let: <S, T>(this: T, block: (thisVal: T) => S) => S | null } Object.prototype.let = function <S, T>(this: T, block: (thisVal: T) => S): S | null { return this != null ? block(this as T) : null } const a = {foo: 42} a.let(it => console.log(it.foo));

Enlace del patio de recreo

over 4 years ago · Santiago Trujillo 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!