[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sinoon opened this issue Dec 26, 2020 · 6 comments
Labels
bug Something isn't working

Comments

@sinoon
Copy link
Contributor
sinoon commented Dec 26, 2020

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

@Lucifier129
Copy link
Collaborator

Thx feedback, I will check it later:)

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

@uinz
Copy link
Contributor
uinz commented Dec 26, 2020
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

@sinoon
Copy link
Contributor Author
sinoon commented Dec 26, 2020
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?

@uinz
Copy link
Contributor
uinz commented Dec 26, 2020

I think it is a bug in typescript

@Lucifier129
Copy link
Collaborator
Lucifier129 commented Dec 26, 2020

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

@Lucifier129 Lucifier129 added the bug Something isn't working label Dec 26, 2020
@tqma113
Copy link
Collaborator
tqma113 commented Jan 27, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants