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

149
Views
Typescript: how to pass variable with complex type to a function without redefining type

I am kind of new to typescript and I had this a few times now. I use for example prisma (or anything) to get a value which's type is monstrously complex (as in long).

It has many attributes and those are perfectly fine. As soon as I want to define a function to handle this value, I lose all the type information since I'd have to redefine this complex type in the parameter.

Example:

    const users = await prisma.user.findMany({
        select:{
            projects: {
                select: {
                    name: true,
                    slug: true,
                    _count:{
                        select: {
                            subscribers: true,
                            mediaIds: true
                        }
                    }
                },
            },
            id: true,
            email: true,
            firstName: true,
            lastName: true,
            createdAt: true,
            _count:{
                select:{
                    mediaIds: true,
                    projects: true
                }
            }
        },
    });

And now I want to define a function to for example handle one single of those subscribers:

users.forEach(user=>{
  // here I have perfect typing for the user object
  handleUser(user)
});
function handleUser(user: <what to put here?>){
  // here I'd have to retype / redefine the monstreously long (but helpful) dynamic type that prisma creates for my query
}

I am confused what the common approach is here.

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

0

If the users array is avaiable in the scope of the function, you could use the typeof operator.

function handleUser(user: typeof users[number]){
  
}

You can index the typeof users with number to get the type of an array element inside users.

about 4 years ago · Juan Pablo Isaza Report

0

You can use the class that define the User model. You define the model one time in the orm file and then you can use the User type to type your parameters. Typescript cannot know that the param user is an user so you have to type it

function handleUser(user: User){
  // handle the user
}
about 4 years ago · Juan Pablo Isaza Report

0

To define a large type in TypeScript, use interface for objects, or type for tiny details.

Example:

interface HumanType {
  name: string;
  age: number;
}

const human: HumanType = { name: "Thomas", age: 34588 }

In this example, if you have a class that defines a Human, use it as a type.

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!