question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript fails to compile the example code in the home page

See original GitHub issue

Describe the bug

I’m replicating the ts code in the home page:

import { setupWorker, rest } from 'msw'
interface LoginBody {
  username: string
}
interface LoginResponse {
  username: string
  firstName: string
}
const worker = setupWorker(
  rest.post<LoginBody, LoginResponse>('/login', (req, res, ctx) => {
    const { username } = req.body
    return res(
      ctx.json({
        username,
        firstName: 'John'
      })
    )
  }),
)
worker.start()

But it won’t compile saying

TypeScript error in /project/src/integration-tests/mocks/handlers.ts(24,7):
Argument of type 'ResponseTransformer<string>' is not assignable to parameter of type 'ResponseTransformer<LoginResponse>'.
  Types of parameters 'res' and 'res' are incompatible.
    Type 'MockedResponse<LoginResponse>' is not assignable to type 'MockedResponse<string>'.
      Type 'LoginResponse' is not assignable to type 'string'.  TS2345

    22 |     const { username } = req.body
    23 |     return res(
  > 24 |       ctx.json({
       |       ^
    25 |         username,
    26 |         firstName: 'John'
    27 |       })

Environment

  • msw: ^0.24.2
  • nodejs: v12.18.0
  • npm: 6.14.4
  • typescript: Version 4.1.3

Please also provide your browser version. Latest edge

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
timdeschryvercommented, Dec 21, 2020

Seems like this is a regression of #468. I’m not sure which way we want/should go with this… using ctx.json returns a string (which is correct), but you lose some type safety with it. I wanted to suggest having a typed ctx.json, but it seems @kettanaito already thought of this 🙂

If my thinking is correct, the example code should look like this:

import { setupWorker, rest } from 'msw'

interface LoginBody {
  username: string
}
interface LoginResponse {
  username: string
  firstName: string
}

const worker = setupWorker(
  rest.post<LoginBody>('/login', (req, res, ctx) => {
    const { username } = req.body
    return res(
      ctx.json<LoginResponse>({
        username,
        firstName: 'John',
      }),
    )
  }),
)
worker.start()

If that’s the case, do you want to file a Pull Request for the docs @haikyuu ?

4reactions
marcosvega91commented, Dec 22, 2020

Thanks for looking into it, @timdeschryver. Do I understand it correctly that type inference is lost because ctx.json returns a string? 🤔 The intention is to infer the type given to rest.post generic.

Yes right, because now the json function will return a string instead of an object of type BodyType.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript Compiling with Visual Studio Code
To generate source maps for your TypeScript files, compile with the --sourcemap option or set the sourceMap property in the tsconfig. json file...
Read more >
Error: TSError: Unable to compile TypeScript - Stack Overflow
In my case, I just delete nodes_modules directory, then I run npm install command, and it worked. Share.
Read more >
TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >
TypeScript: JavaScript With Syntax For Types.
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. Property 'name' does not exist...
Read more >
TypeScript configuration - Angular
Browsers can't execute TypeScript directly. Typescript must be "transpiled" into JavaScript using the tsc compiler, which requires some configuration. This page ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found