роститите за тупой вопрос просто приходится учить ts походу дела. Как предать instance объекта в функцию? Вот в одном файле експротирую инстанс класса вот так
class ApiError extends Error{
constructor(status, message) {
status: number;
message: string;
super();
this.status = status
this.message = message
}
static badRequest(message) {
return new ApiError(404, message)
}
static internal(message) {
return new ApiError(500, message)
}
static forbidden(message) {
return new ApiError(403, message)
}
}
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){
if (err instanceof ApiError) {
return res.status(err.status).json({message: err.message});
}
return res.status(500).json({message: "Непредвиденная ошибка!"})
}
export default errorHandler;
И вот получаю:
'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){
~~~~~~~~
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/Test Task by Komah Ihor/node_modules/ts-node/src/index.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...
если прердавать через обращение напрямую
export default ApiError();
то выдает
/home/ihor/Test Task by Komah Ihor/node_modules/ts-node/src/index.ts:587
return new TSError(diagnosticText, diagnosticCodes);
^
TSError: ⨯ Unable to compile TypeScript:
src/middlewares/ErrorHandlingMiddleware.ts:6:31 - error TS2339: Property 'status' does not exist on type 'ApiError'.
6 return res.status(err.status).json({message: err.message});
~~~~~~
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/Test Task by Komah Ihor/node_modules/ts-node/src/index.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...
Что делать? как быть как?