In TypeScript, 2.2...
请允许我说,我有一个人类型:
interface Person {
name: string;
hometown: string;
nickname: string;
}
而我也希望创造一种恢复一个人的工作功能,但并不需要一个镍:
function makePerson(input: ???): Person {
return {...input, nickname: input.nickname || input.name};
}
What should be the type of input
? I m looking for a dynamic way to specify a type that is identical to Person
except that nickname
is optional (nickname?: string | undefined
). The closest thing I ve figured out so far is this:
type MakePersonInput = Partial<Person> & {
name: string;
hometown: string;
}
但是,这并不是我所期望的,因为我必须具体说明所有类型的要求<>/em>,而不是选择的类型。