Typescript fails to compile the example code in the home page
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:4
- Comments:8 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 typedctx.json
, but it seems @kettanaito already thought of this 🙂If my thinking is correct, the example code should look like this:
If that’s the case, do you want to file a Pull Request for the docs @haikyuu ?
Yes right, because now the
json
function will return a string instead of an object of typeBodyType
.