Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第一题 #91

Open
1596944197 opened this issue Feb 20, 2022 · 0 comments
Open

第一题 #91

1596944197 opened this issue Feb 20, 2022 · 0 comments

Comments

@1596944197
Copy link

因为t并不等于User,t是User的子类,所以t可能会有更多的属性,而这只是返回了User,可以这样修改👇

type User = {
  id: number;
  kind: string;
};
function makeCustomer<T extends User>(u: T):User  {
  return {
    id: u.id,
    kind: 'customer'
  }
}

function makeCustomer<T extends User>(u: T):T  {
  return {
    ...u,// 这里u拥有T的全部属性,返回T的扩展也是可以的
    id: u.id,
    kind: 'customer'
  }
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant