简体   繁体   English

Typescript:如何在不重新定义类型的情况下将复杂类型的变量传递给 function

[英]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.我对 typescript 有点陌生,我现在有几次。 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.一旦我想定义一个 function 来处理这个值,我就会丢失所有类型信息,因为我必须在参数中重新定义这个复杂类型。

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:现在我想定义一个 function 来例如处理其中一个订阅者:

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.我很困惑这里的常用方法是什么。

You can use the class that define the User model.您可以使用定义User model 的 class。 You define the model one time in the orm file and then you can use the User type to type your parameters.您在 orm 文件中定义一次 model,然后您可以使用User类型来键入您的参数。 Typescript cannot know that the param user is an user so you have to type it Typescript 无法知道参数用户是用户,因此您必须输入

function handleUser(user: User){
  // handle the user
}

To define a large type in TypeScript, use interface for objects, or type for tiny details.要在 TypeScript 中定义大类型,请对对象使用interface ,或为微小细节使用type

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.在此示例中,如果您有一个定义 Human 的 class,请将其用作类型。

If the users array is avaiable in the scope of the function, you could use the typeof operator.如果users数组在 function 的 scope 中可用,则可以使用typeof运算符。

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

You can index the typeof users with number to get the type of an array element inside users .您可以使用number索引typeof users的类型以获取users内部数组元素的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM