Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda