我正在使用 JavaScript 和 Typescript 進行開發。我有下面的函式來檢查一個陣列是否有重復,但我收到一個錯誤,我不確定如何解決。以下是錯誤和代碼摘錄。
錯誤:“注冊”型別上不存在屬性“toLocaleLowerCase”.ts(2339)
注冊.ts
export interface Registration {
address: string;
comment?: string;
fullname?: string;
}
JS檔案
const nameAlreadyExist = (name: any): void => {
const nameExist = filteredRegistrationName.value.findIndex((registrationName) =>
registrationName.fullname.toLocaleLowerCase() === name.toLocaleLowerCase());
nameExist != -1 ? (existNameError.value = true) : (existNameError.value = false);
};
任何見解將不勝感激。謝謝!
uj5u.com熱心網友回復:
這正是它的意思——它在你的Registration型別上不存在。toLocaleLowerCase()只存在于 type string- 所以除非你可以將Registrationtype 映射到 a string,否則它將不起作用。我看到這Registration.fullname是一個字串,但它也是可選的 - 這意味著它可能是未定義的,這也可能引發錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/522079.html
下一篇:打字稿只讀和getter方法
