Как передать instace объекта в функцию?
Проститите за тупой вопрос просто приходится учить ts походу дела. Как предать instance объекта в функцию?
Вот в одном файле експротирую инстанс класса вот так export default new ApiError();
и в другом использую:
import { Request, Response, NextFunction } from "express";
import ApiError from '../errors/ApiError';
const errorHandler = function (err: ApiError, req: Request, res: Response, next: NextFunction): Response {
if (err instanceof ApiError) {
return res.status(err.status).json({message: err.message});
}
return res.status(500).json({message: "Непредвиденная ошибка!"})
}
И мне высвечивает ошибка типа это не тип
: ⨯ Unable to compile TypeScript:
src/middlewares/ErrorHandlingMiddleware.ts:4:37 - error TS2749: 'ApiError' refers to a value, but is being used as a type here. Did you mean 'typeof ApiError'?
4 const errorHandler = function (err: ApiError, req: Request, res: Response, next: NextFunction): Response {
~~~~~~~~
src/middlewares/ErrorHandlingMiddleware.ts:5:24 - error TS2359: The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type.
5 if (err instanceof ApiError) {
~~~~~~~~
at createTSError (/home/ihor/Test Task by Komah Ihor/node_modules/ts-node/src/index.ts:587:12)
at reportTSError (/home/ihor/Test Task by Komah Ihor/node_modules/ts-node/src/index.ts:591:19)
at getOutput (/home/ihor/Test Task by Komah Ihor/node_modules/ts-node/src/index.ts:921:36)
at Object.compile (/home/ihor/Test Task by Komah Ihor/node_modules/ts-node/src/index.ts:1189:32)
at Module.m._compile (/home/ihor/T^: команда не найдена
est Task by Komah Ihor/node_modules/ts-node/src/inihor@ihor-GF65-Thin-9SEXR:~/Test Task by Komah Ihor$ TSError: ⨯ Unable to compile TypeScript:
dex.ts:1295:42)
at Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Object.require.extensions.<computed> [as .ts] (/home/ihor/Test Task by Komah Ihor/node_modules/ts-node/src/index.ts:1298:12)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module._load (internal/modules/cjs/loader.js:774:14)
at Module.require (internal/modules/cjs/loader.js:957:19)
[nodemon] app crashed - waiting for file changes before starting...
^CTSError:: команда не найдена
как использовать тип ApiError в єтом файле