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

194
Views
Typescript passing a type as a parameter

using Directus SDK. Im trying to do a function that does this, I just want to change the type(CollectionsNameChanges) everytime the function is called

const directus = new Directus<CollectionsNameChanges>('https://mysamelink/');

For this I created this function

connection.ts

//here I want to get a new Type, that's why I am passing title as a parameter

export function DirectusConnection(title: any) {
  return new Directus<typeof title>('https://mysamelink/');
}

myList.ts

interface MyTypes {
  id: number;
}

export async function myFunction(){
  type CollectionsNameChanges= {
    //collection name
    nameofCollection: MyTypes;
  };

//here I am calling my function with the type
  const directus = DirectusConnection(<CollectionsNameChanges>);
  const myList = await directus.items('nameofCollection').readMany();

  return myList 
}

But I am getting an error in typescript telling me that expression was expected. here:

  const directus = DirectusConnection(<DirectusCollection>);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should use a generic parameter:

export function DirectusConnection<T>() {
  return new Directus<T>('https://mysamelink/');
}

// or if you want to use a parameter as that generic type
// but `title` isn't being used anywhere in this function
export function DirectusConnection<T>(title: T) {
  return new Directus<T>('https://mysamelink/');
}

// then later:
const directus = DirectusConnection<CollectionsNameChanges>();

Generics documentation.

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!