error TS2571: Object is of type 'unknown'
VittorioAccomazzi opened this issue · comments
I'm running in to this error when starting a project as follow:
npx express-generator-typescript --with-auth "express-ts-auth"
and then when compiling:
node ➜ /workspaces/express-ts-auth $ npm run build
> express-ts-auth@0.0.0 build
> ./node_modules/.bin/ts-node build.ts
INFO: src/routes/middleware.ts(32,20): error TS2571: Object is of type 'unknown'.
ERROR: Error: Command failed: tsc --build tsconfig.prod.json
node ➜ /workspaces/express-ts-auth $ node -v
v16.8.0
node ➜ /workspaces/express-ts-auth $ npm -v
7.21.0
node ➜ /workspaces/express-ts-auth $
also when I ran the test I encounter this error:
/workspaces/express-ts-auth/node_modules/ts-node/src/index.ts:692
return new TSError(diagnosticText, diagnosticCodes);
^
TSError: ⨯ Unable to compile TypeScript:
spec/index.ts:20:29 - error TS2345: Argument of type 'null' is not assignable to parameter of type 'JasmineOptions'.
20 const jasmine = new Jasmine(null);
I'm using a container for the compilation.
The error reported is the following :
and it can be fixed as follow:
} catch (err:any) {
return res.status(UNAUTHORIZED).json({
error: err.message,
});
}
I'm not sure however if this is the correct fix.
The error in the tests can be corrected simply as follow:
// Init Jasmine
const jasmine = new Jasmine({});
the {}
is currently missing.
Or just remove .message
} catch (err) {
return res.status(UNAUTHORIZED).json({
error: err,
});
}
or check if it's an instance of Error
} catch(err: unknown) {
if (err instanceof Error) {
return res.status(UNAUTHORIZED).json({
error: err.message
})
}
return res.status(UNAUTHORIZED).json({
error: err
})
}
fixed