我有這個問題為什么我總是要填寫詳細資訊
type Doc = Record<string,any>
type DocFilter<T extends Doc> = {[k in keyof T as T[k] extends (()=>void) ? never:k]:T[k]}
const doc1 = {aaaa:'',bbbb:1,c:()=>{}}
function test1 <T extends Partial< typeof doc1>,>(opt:{prop:T}){}
test1({
prop:{
aaaa:'',
bbbb:1, // When I enter the "bbbb" field, there is a prompt .This was to be expected
}
})
test2 我要過濾掉函式欄位 // 為什么我總是要填寫詳細資訊
function test2 <T extends Partial<DocFilter< typeof doc1>>,>(opt:{prop:T}){}
test2({
prop:{
aaaa:'', // When I enter the first field, the correlation keyword prompts all fields. For example, enter a to prompt aaaa and b to prompt bbbb
b //But I couldn't get a prompt when I entered the second field .The cause was found because of the ''as' keyword. How to solve this problem?
}
})
操場
uj5u.com熱心網友回復:
我不確定為什么您的解決方案不起作用,但是這種另一種定義方式DocFilter似乎可以滿足您的需求。as您可以使用Omit排除您不想要的鍵,而不是使用重新映射鍵。我KeysMatching從另一個答案中借用了。
type KeysMatching<T, V> = {[K in keyof T]: T[K] extends V ? K : never}[keyof T];
type DocFilter<T> = Omit<T, KeysMatching<T, () => void>>
游樂場鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/343885.html
標籤:打字稿
下一篇:打字稿`super`實作不提供值
