farrow
farrow copied to clipboard
Type Question: Response.json get type error: is not assignable to type 'undefined'.ts(2345)
I got a type error, however, I don't have any idea about is.
Can you help me?
There are npm type:
export interface NPM {
name: string
"dist-tag": {
[tag: string]: string
}
maintainers: {
name: string
email: string
avatar: string
}[]
time: {
[version: string]: string
}
author: {
name: string
email: string
}
license: string
}
import { Response, Router } from "farrow-http"
export const router = Router()
router
.match({
pathname: "/search",
query: {
search: String,
limit: Number,
},
})
.use(async request => {
const { search, limit } = request.query
// search
const details = await getPackageDetail(search)
return Response.json({
code: 0,
data: details,
})
})
And will got
Argument of type '{ code: number; data: NPM[]; }' is not assignable to parameter of type 'JsonType'.
Type '{ code: number; data: NPM[]; }' is not assignable to type 'undefined'.ts(2345)

Thx feedback, I will check it later:)
// for now, use `as` for workaround
return Response.json({
code: 0,
data: details,
} as JsonType)
export interface NPM { name: string "dist-tag": { [tag: string]: string } maintainers: { name: string email: string avatar: string }[] time: { [version: string]: string } author: { name: string email: string } license: string }
export type NPM = {
name: string
"dist-tag": {
[tag: string]: string
}
maintainers: {
name: string
email: string
avatar: string
}[]
time: {
[version: string]: string
}
author: {
name: string
email: string
}
license: string
}
use type instead of interface
https://www.typescriptlang.org/play?#code/C4TwDgpgBAClC8UDeUDaBrCIBcUDOwATgJYB2A5gLq5wA++RZ5UAvgFABmArqQMbDEA9qSgdSACjA0AlMlZsFZYBEIcAhr2gBBZGyj6opNQFsIuAiQpt2bJSvWaoAIV0G0mHA0tVzjKzdBIKABhBFcDDCxfb2ovJmsFQOgAETCkPQMjU2j4mzYAGwhgKDU0lhK8KC0CoqgAIzKK5xri3ka1SuCWqAATdsrkhTFxNWlOCTqx4d4piR6xhSA
export interface NPM { name: string "dist-tag": { [tag: string]: string } maintainers: { name: string email: string avatar: string }[] time: { [version: string]: string } author: { name: string email: string } license: string }export type NPM = { name: string "dist-tag": { [tag: string]: string } maintainers: { name: string email: string avatar: string }[] time: { [version: string]: string } author: { name: string email: string } license: string }use type instead of interface
https://www.typescriptlang.org/play?#code/C4TwDgpgBAClC8UDeUDaBrCIBcUDOwATgJYB2A5gLq5wA++RZ5UAvgFABmArqQMbDEA9qSgdSACjA0AlMlZsFZYBEIcAhr2gBBZGyj6opNQFsIuAiQpt2bJSvWaoAIV0G0mHA0tVzjKzdBIKABhBFcDDCxfb2ovJmsFQOgAETCkPQMjU2j4mzYAGwhgKDU0lhK8KC0CoqgAIzKK5xri3ka1SuCWqAATdsrkhTFxNWlOCTqx4d4piR6xhSA
Yeah, that's ok, Thank you!
is interesting why interface is type error but type is ok?
I think it is a bug in typescript
@sinoon @uinz Maybe we can file an issue for Typescript to see whether is a bug or not:-)
https://github.com/microsoft/TypeScript/issues/15300#issue-223240276