當 enablePagination 為真時,有什么方法可以在 PaginationProps 中設定屬性?
這可能嗎?
interface PaginationProps {
total?: number;
offset?: number;
limit?: number;
}
interface Props<T> extends PaginationProps {
data: T;
enablePagination: boolean;
}
uj5u.com熱心網友回復:
我希望我能回答這個問題。也許我會嘗試它(但不是現在)。
我認為您需要將Required 實用程式型別與條件型別結合使用。
編輯 感謝@Federkun - 這是解決方案:
interface PaginationProps {
total?: number
offset?: number
limit?: number
}
type OptionalPaginationProps = ({ enabledPagination: false } & PaginationProps) | ({ enabledPagination: true } & Required<PaginationProps>)
type Props<T> = OptionalPaginationProps & { data: T; }
const propsNoPagination: Props<string> = {
data: 'hello',
enabledPagination: false
}
const propsWithPagination: Props<string> = {
data: 'hello',
enabledPagination: true,
total: 1,
offset: 2,
limit:3
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/439051.html
上一篇:{}型別的打字稿錯誤引數不可分配給用戶型別的引數|用戶()=>用戶
下一篇:AndroidStudio回傳錯誤“安裝失敗,原因是:''cmdpackageinstall-create...”
