It's really good to see a web framework that deals with TypeScript as a first class citizen! I'm really excited about this project because of this.
However, when turning the option: noImplicitAny
in the tsconfig.json
the compiler will produce warnings about a lot of the examples in the documentation.
I highly recommend adding Express TypeScript declarations to the examples like so:
import {Request,Response,NextFunction} from "@types/express";
@Controller({})
export class UsersController {
getAllUsers(req:Request, res:Response, next:NextFunction) {
// will have auto completion, type safety ...etc
}
}
To solve the problem:
npm install --save-dev @types/express
I think we can close this issue.
@ThomRick He's asking that the examples on https://kamilmysliwiec.gitbooks.io/nest/content/ add the Express types. Right now they just say:
@Get('/:id')
public async getUser(@Response() res, @Param('id') id) {
as opposed to
@Get('/:id')
public async getUser(@Response() res: Response, @Param('id') id) {
unfortunately not possible to import Request, Response
from @types/express
and nest.js
together, have to alias one of them
import { Response, Request } from '@types/express';
import {
Response as Req,
Request as Res
} from 'nest.js';
When trying to do the above I get:
src/index.ts(1,8): error TS1192: Module '"C:/Development/me/nest-graph-api/node_modules/@types/express/index"' has no default export.
src/users/users.controller.ts(11,18): error TS4053: Return type of public method from exported class has or is using name 'Response' from external module "C:/Development/me/nest-graph-api/node_modules/@types/express-serve-static-core/index" but cannot be named.
src/users/users.controller.ts(17,18): error TS4053: Return type of public method from exported class has or is using name 'Response' from external module "C:/Development/me/nest-graph-api/node_modules/@types/express-serve-static-core/index" but cannot be named.
src/users/users.controller.ts(23,18): error TS4053: Return type of public method from exported class has or is using name 'Response' from external module "C:/Development/me/nest-graph-api/node_modules/@types/express-serve-static-core/index" but cannot be named.
Here is the code I'm trying to transpile
import { Response, Request } from '@types/express';
import { Controller, Get, Post, Request as Req, Response as Res, Param, Body, HttpStatus } from 'nest.js';
import { UsersService } from './users.service';
@Controller('users')
export class UsersController {
constructor(private usersService: UsersService) {}
@Get()
public async getAllUsers(@Res() res: Response) {
const users = await this.usersService.getAllUsers();
return res.status(HttpStatus.OK).json(users);
}
@Get('/:id')
public async getUser(@Res() res: Response, @Param('id') id) {
const user = await this.usersService.getUser(id);
return res.status(HttpStatus.OK).json(user);
}
@Post()
public async addUser(@Res() res: Response, @Body('user') user) {
const msg = await this.usersService.addUser(user);
return res.status(HttpStatus.CREATED).json(msg);
}
}
This was addressed by #36
Since latest version @Request()
and @Response()
decorators have an aliases - @Req()
and @Res()
so there is no collision with express typings. About @types/express
, I added short notice about it in the documentation.
I found an issue with decorators being used on function parameters that might be related to this.
when using something like
@Get('playlist')
async getPlaylist(@Res() res) {
res.send(playlist);
}
This will only work so long as your typescript's tsconfig.json is targeting es2015 or higher, if targeting es5, this will throw an error like so:
[Nest] 17957 - 2017-8-2 22:14:37 [ExceptionsHandler] res.send is not a function
TypeError: res.send is not a function
at PlaylistController.<anonymous> (/Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:118:43)
at step (/Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:44:23)
at Object.next (/Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:25:53)
at /Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:19:71
at Promise (<anonymous>)
at __awaiter (/Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:15:12)
at PlaylistController.getPlaylist (/Users/mark.mccracken/Work/learning/angular/state-management/dist/server/playlist/playlist.controller.js:116:16)
at /Users/mark.mccracken/Work/learning/angular/state-management/node_modules/@nestjs/core/router/router-proxy.js:7:33
at Layer.handle [as handle_request] (/Users/mark.mccracken/Work/learning/angular/state-management/node_modules/@nestjs/core/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/mark.mccracken/Work/learning/angular/state-management/node_modules/@nestjs/core/node_modules/express/lib/router/route.js:131:13)
::1 - - [02/Aug/2017:21:14:37 +0000] "GET /api/playlist HTTP/1.1" 500 31
Hope this might help anyone coming behind me with a really simple solution
This will only work so long as your typescript's tsconfig.json is targeting es2015 or higher, if targeting es5, this will throw an error like so...
This is happening to me also with ES6 in tsconfig.json.
When it was said that this framework works in TypeScript I expected it to support express types integrally. For example - I don't need to import the express module because NestJS does that for me. Why than would I need to import express types? The types should be imported and inferred automatically when I use the @Req decorator.
@ShacharHarshuv that's not how decorators work in TS.
Even if they could infer with return types, there's still an issue, because Nest supports both Fastify and Express.
What if I'll just need to write the type (e.g. Request) without a decorator and Nest will give me the request like happens with dependency injection? (You ask for a class and the injector supplies you with an instant.)
Is that something that might be possible to implement?
Then the server package would still need to have both Fastify and Nest platform installed for it to work, and Nest still supports JS, so that's not an option either, plus it's more a downgrade than an actual bug fix imo.
My advice? Stop being lazy.
Any update on this? I still face the same issue when targeting es2017. Using Nest 6.5.3
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Then the server package would still need to have both Fastify and Nest platform installed for it to work, and Nest still supports JS, so that's not an option either, plus it's more a downgrade than an actual bug fix imo.
My advice? Stop being lazy.