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

266
Views
TypeScript using "this" keyword in interface

I'm working on a custom interface where I'd like to be able to infer the type of an interface value in its implementation, rather than in the interface definition, without generics. I should add that these implementations will always be object literals and not classes.

Here is what I'm trying to do:

interface Defintion {
  params: unknown; // Should resolve to the type in the implementation
  handler: (dummy: this["params"]) => void; // I want `this` to refer to the implementation, not the interface
}


const customDefn: Defintion = {
  params: { test: "Hello World" },
  handler: (dummy) => {
    // TS Complains here
    console.log(dummy.test)
  }
}

What I want to happen is the type of customDefn.params to resolve to { test: "Hello World" } (so I can get autocomplete in handler) rather than remaining as unknown.

I am aware I can use generics:

interface Defintion<T> {
  params: T;
  handler: (dummy: this["params"]) => void;
}

However, I would then have to define params twice (since I use params programmatically as well):

const customDefn: Defintion<{ test: "Hello World" }> = {
  params: { test: "Hello World" },
  handler: (dummy) => {
    console.log(dummy.test)
  }
}

Which can become cumbersome.

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

0

The keyword this can not be used to reference other properties in an interface. In fact, using a stand-alone type, it is not possible to do any cross referencing between properties.

Instead, you can achieve this with a generic helper function.

interface Defintion<T> {
  params: T;
  handler: (dummy: T) => void; 
}

function createDefiniton<T>(definiton: Defintion<T>) {
  return definiton
}

createDefiniton({
  params: { test: "Hello World" },
  handler: (dummy) => {
    console.log(dummy.test)
  }
})

Playground

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!