Почему я получаю ошибку при использовании типа Promise . к асинхронным функциям. ошибаться В функции отсутствует конечный оператор возврата, а тип возвращаемого значения не включает «неопределенный».
import { faceProductList } from "../../Type/Interface";
class Server {
private url: string = "https://...../";
public async request(id: string): Promise<object | string> {
try {
const res = await fetch(`${this.url}${id}`);
if (!res.ok) {
throw Error("Page Not Found 404");
}
const resArr: object = await res.json();
return resArr;
} catch (error) {
return error.message;
}
}
public async handler(
pathname: string,
valueSearch: string
): Promise<faceProductList[] | string> {
try {
const prodObj = await this.request(pathname);
if (typeof prodObj === "string") {
throw Error(prodObj);
} else if (valueSearch) {
return Object.values(prodObj)
.flat()
.filter(({ title }) => title.includes(valueSearch));
}
} catch (error) {
return error;
}
}
}





В случае, если prodObj не типа string и valueSearch ложно (например, пустая строка) - функция неявно возвращает undefined. Вы должны решить, что делать (вернуться) в этом случае.
if (typeof prodObj === "string") {
throw Error(prodObj);
} else if (valueSearch) {
return Object.values(prodObj)
.flat()
.filter(({ title }) => title.includes(valueSearch));
}
// undefined returned here implicitly
A function without a return statement will return a default value. In the case of a constructor called with the
newkeyword, the default value is the value of itsthisparameter. For all other functions, the default return value isundefined.