我在删除属性方面遇到问题。 例如:
type Person<GN> = {
getName: GN extends never ? never : GN,
}
const foo = <GN>(person: Person<GN>) => person
const first = foo({}) // should work
const second = foo({
getName: (name: string) => name,
})
在这种情况下,首先不需要getName属性。我该怎么解决。
使用可选的“?”属性会导致输出不清楚。 例如
type Person<GN> = {
getName?: GN,
}
const foo = <GN>(person: Person<GN>) => person
const first = foo({}) // should work
first.getName // shouldn't exist 'getName' => but still have
const second = foo({
getName: (name: string) => name,
})
second.getName // should exist getName but in this case is optional property
如何清楚地输出? 感谢您的阅读。
答案 0 :(得分:2)
因此,您希望将first
类型推断为{}
,并将second
类型推断为{ getName: (name: string) => string; }
吗?
这是一种实现方法:
type Person<GN> = { getName: GN }
const foo = <GN, P extends Partial<Person<GN>>>(person: P) => person;
const first = foo({}) // const first: {}
const second = foo({ // const second: { getName: (name: string) => string; }
getName: (name: string) => name,
})