簡體   English   中英

如何將類的屬性作為對象文字類型獲取?

[英]How do I get the properties of a class as object literal type?

下面我有一個擴展JokeSignup類。

我想要的是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM