farrow icon indicating copy to clipboard operation
farrow copied to clipboard

Type Question: Response.json get type error: is not assignable to type 'undefined'.ts(2345)

Open sinoon opened this issue 4 years ago • 6 comments

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)

image

sinoon avatar Dec 26 '20 02:12 sinoon

Thx feedback, I will check it later:)

// for now, use `as`  for workaround
return Response.json({
      code: 0,
      data: details,
  } as JsonType)

Lucifier129 avatar Dec 26 '20 02:12 Lucifier129

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

uinz avatar Dec 26 '20 03:12 uinz

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?

sinoon avatar Dec 26 '20 04:12 sinoon

I think it is a bug in typescript

uinz avatar Dec 26 '20 08:12 uinz

@sinoon @uinz Maybe we can file an issue for Typescript to see whether is a bug or not:-)

Lucifier129 avatar Dec 26 '20 08:12 Lucifier129

https://github.com/microsoft/TypeScript/issues/15300#issue-223240276

tqma113 avatar Jan 27 '21 17:01 tqma113