如何键入返回对象的常量函数?

时间:2019-11-13 05:43:04

标签: javascript typescript

这是我的代码:

interface profileProps {
  id: string
  name: string
  email: string
  link: string
}
const profile = await queryFacebookAPI("/me", { fields: "name,email,link" });

我尝试过:

const profile: () => profileProps = await queryFacebookAPI("/me", { fields: "name,email,link" })
const profile = await queryFacebookAPI("/me", { fields: "name,email,link" }):profileProps;

它仍然不起作用

console.log('profile.name',profile.name)

我收到一个错误,指出该属性名称在void类型上不存在?

2 个答案:

答案 0 :(得分:0)

check the Demo

import { Component } from "@angular/core";
interface profileProps {
  id: string;
  name: string;
  email: string;
  link: string;
}
@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular";
  profile = {} as profileProps;

  print() {
    this.profile.name = "Name";
    console.log(this.profile.name);
  }
}

答案 1 :(得分:0)

async function getFbProfile(fields: string = "name,email,link"): Promise<profileProps> {
   const res = await await queryFacebookAPI("/me", { fields });
   const profileData = await res.json();

   return profileData;
}
相关问题