我有以下 Typescript 代碼,IDE 告訴我“此運算式不可呼叫。型別 'Pick<IUserContext, “clearAllLoginData">' 沒有呼叫簽名。” 但我不明白為什么。
據我了解,函式的clearAllLoginData引數logout應該是 type () => void,因為它是Pick從IUserContext介面編輯的。
任何指標表示贊賞!
interface IUserContext {
id?: number;
email?: string;
fname?: string;
lname?: string;
slname?: string;
companyId?: number;
companyName?: string;
clearAllLoginData: () => void;
setAllLoginData: () => void;
}
interface LogoutParams {
clearAllLoginData: Pick<IUserContext, "clearAllLoginData">;
afterLogout?: () => void;
}
export const logout = ({ clearAllLoginData, afterLogout }: LogoutParams):void => {
clearAllLoginData(); // <---- Error: "This expression is not callable"
if (afterLogout) {
afterLogout();
}
};
uj5u.com熱心網友回復:
什么Pick是
通過從 Type 中選擇一組屬性 Keys(字串文字或字串文字的聯合)來構造一個型別。
換句話說 - 它從另一個物件型別創建一個物件型別。
Pick<IUserContext, "clearAllLoginData">
將產生一個型別:
{
clearAllLoginData: () => void;
}
它不會給你一種
() => void
Pick這不是正確的工具。你需要這樣的東西:
interface LogoutParams {
clearAllLoginData: IUserContext["clearAllLoginData"]
afterLogout?: () => void;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/454817.html
標籤:打字稿
