简体   繁体   English

Axios 和 Typescript:如何从 Object 分配类型变量

[英]Axios and Typescript : How assign a typed variable from Object

I'm currently working on a Next js App and i'm using Axios to call my external API.我目前正在开发 Next js 应用程序,我正在使用 Axios 来调用我的外部 API。

When i'm using Axios i can define a type to template the function like this:当我使用 Axios 时,我可以定义一个类型来模板 function,如下所示:

export type User = {
  id: string
  username: string
  firstName: string
  lastName: string
}

const response = await axios.get<User>(route, config)

But if the API send me more properties on data, the user is not a User anymore is just an object.但如果 API 向我发送更多关于数据的属性,则用户不再是用户,只是 object。

For example:例如:

Data received from the API从 API 收到的数据

{
  id: "foo"
  username: "foo"
  firstName: "foo"
  lastName: "foo"
  email: "foo" // not in User
}

Then i will assign my user variable with the data然后我将用数据分配我的用户变量

const user: User = response.data // should be a User 

Finally, if i log my user there is spare propeties like email最后,如果我登录我的用户,就会有像 email 这样的备用属性

console.log(user)

// give me 

{
  id: "foo"
  username: "foo"
  firstName: "foo"
  lastName: "foo"
  email: "foo"
}

// and not 

{
  id: "foo"
  username: "foo"
  firstName: "foo"
  lastName: "foo"
}

So what is the interest of the template in the get function?那么get function中的模板有什么用呢?

and

Is there a generic way to assign my typed variable with the object received and choose between fill my type with the value received and ignore spare propeties or fill my type with the value received and if there is one or more propeties in extra just return or throw an error?有没有一种通用的方法来分配我的类型变量与收到的 object 并选择用收到的值填充我的类型并忽略备用属性或用收到的值填充我的类型,如果有一个或多个额外的属性,只需返回或抛出一个错误?

Thanks谢谢

You can use type narrowing for this.您可以为此使用类型缩小。

  const isUser = (candidate: any): candidate is User => {
      return (
          ('id' in candidate && typeof candidate.id === 'string') && 
          ('username' in candidate && typeof candidate.username === 'string') 
          // etc
      ) 
  }

if (!isUser(dataFromApi)) {
    throw new Error("Bad data structure")
}

After the isUser your code knows it is a User.在 isUser 之后,您的代码知道它是一个用户。 In practice I like to use superstruct for this.在实践中,我喜欢为此使用上层结构

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

相关问题 解构 object 允许将不正确的值分配给 TypeScript 中的类型化变量 - Destructuring object allows to assign incorrect value to typed variable in TypeScript 如何创建 object 并将其分配给严格类型的变量? - How to create an object and assign it to a strictly typed variable? 如何在 Typescript 中声明类型化的 object 变量 - How to declare a typed object variable in Typescript 类型化变量的 TypeScript 空对象 - TypeScript empty object for a typed variable 如何从 TypeScript 中的未知类型 object 中读取密钥 - How to read key from unknown typed object in TypeScript 如何从 TypeScript 中键入的 object 中动态删除密钥 - How to dynamically remove keys from a typed object in TypeScript 如何从 Typescript 模式创建 GraphQL 类型 SDK? - How to create a GraphQL typed SDK from a Typescript schema object? 如何在 JavaScript 中将对象中所有元素的类型(typeof)分配给变量 | 打字稿? - How to assign types of (typeof) all elements in the object to a variable in JavaScript | Typescript? TypeScript - 如何在一个语句中分配给变量之前检查 object 是否存在? - TypeScript - How to check if object exist before assign to variable in one statement? 如何从axios响应中在对象的react typescript数组中设置数据 - How to set data in react typescript array of object from axios response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM