[英]How do I get the properties of a class as object literal type?
下面我有一個擴展Joke
的Signup
類。
我想要的是v
有一個類型錯誤,其中 username 不能是數字,但通用ObjectProps
沒有正確獲取Signup
的道具。
type Constructor = new (...args: any[]) => {}
type ObjectProps<T extends Constructor, V extends Constructor | undefined = undefined> = Omit<{
[k in keyof T]: T[k]
}, keyof V>
class Joke {
anythingNotInOriginal: boolean = true
}
class Signup extends Joke {
email: string = ''
username: string = ''
password: string = ''
}
type x = ObjectProps<typeof Signup, typeof Joke>
const v: x = {
username: 1
}
我怎樣才能讓x
具有以下簽名:
{ email: string, username: string, password: string }
並從Signup
類派生?
正如@jcalz 在評論中提到的,這是有效的
const v: Omit<Signup, keyof Joke> = {
email: '',
username: '',
password: ''
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.