GithubHelp home page GithubHelp logo

[Documentation] Express Typing about nest HOT 13 CLOSED

nestjs avatar nestjs commented on April 25, 2024 8
[Documentation] Express Typing

from nest.

Comments (13)

KerryRitter avatar KerryRitter commented on April 25, 2024 2

@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) {

from nest.

marcus-sa avatar marcus-sa commented on April 25, 2024 2

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.

from nest.

thomrick avatar thomrick commented on April 25, 2024

To solve the problem:

npm install --save-dev @types/express

I think we can close this issue.

from nest.

jakub-gawlas avatar jakub-gawlas commented on April 25, 2024

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';

from nest.

ScottMGerstl avatar ScottMGerstl commented on April 25, 2024

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

from nest.

kamilmysliwiec avatar kamilmysliwiec commented on April 25, 2024

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.

from nest.

Mark-McCracken avatar Mark-McCracken commented on April 25, 2024

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

from nest.

carloscasalar avatar carloscasalar commented on April 25, 2024

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.

from nest.

ShacharHarshuv avatar ShacharHarshuv commented on April 25, 2024

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.

from nest.

marcus-sa avatar marcus-sa commented on April 25, 2024

@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.

from nest.

ShacharHarshuv avatar ShacharHarshuv commented on April 25, 2024

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?

from nest.

ThenmozhiGit avatar ThenmozhiGit commented on April 25, 2024

Any update on this? I still face the same issue when targeting es2017. Using Nest 6.5.3

from nest.

lock avatar lock commented on April 25, 2024

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.

from nest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.