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

218
Views
typescript generics interface, method parameter and method return type

I have this function

function getCollection<T>(collectionType: T): Collection<T> {
  return new Collection<T>()
}

and in Collection class I have this

export class Collection<T> {
  public add (item: T) {
    // .. logic
  }
}

I have a user interface defined like this

export interface IStudent {

}

and when I attempt to do

getCollection(IStudent).add({});

There is an error

TS2693: 'IStudent' only refers to a type, but is being used as a value here.

How do I make it accept generic type and returned strictly typed Collection?

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

0

A generic type is a type parameter, not a function parameter. You define

function getCollection<T>(): Collection<T> {
  return new Collection<T>()
}

(notice collectionType was an unused parameter anyway) and then call it as

getCollection<IStudent>().add({});
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!