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

[TypeScript] Type '{}' is not assignable to type '<Type>'. Property '<property>' is missing in type '{}'. #371

Closed
acro5piano opened this issue Aug 14, 2018 · 0 comments · Fixed by #401

Comments

@acro5piano
Copy link

acro5piano commented Aug 14, 2018

Hi, Thank you for the really great framework. I love zeit/micro which brings me a lot of productivity.

Environment

package.json

    "@types/micro": "^7.3.1",
    "typescript": "^3.0.1"

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "lib": ["es2015"],
    "strict": true,
    "noUnusedLocals": true,
    "baseUrl": "./",
    "typeRoots": [
      "node_modules/@types"
    ],
    "paths": {
      "*": [ "./src/*" ]
    }
  },
  "include": [
    "./src/**/*",
    "./src/**/__tests__/*.test.ts"
  ]
}

Problem

I use Micro with TypeScript. When I decode request body using json, I got this error:

Type '{}' is not assignable to type 'Transaction'.  Property 'sender' is missing in type '{}'.

the code:

index.ts

import { json } from 'micro'
import { IncomingMessage } from 'http'

interface Transaction {
  sender: string
}

const newTransaction = async (req: IncomingMessage) => {
  const transaction: Transaction = await json(req)
}

Possible solution

This may be because json returns Promise<object>.

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/micro/index.d.ts#L26

If I update the definition using Generic Type like the following code, the error disappeared.

@types/micro/index.d.ts

export function json<T extends {} = {}>(req: IncomingMessage, info?: { limit?: string, encoding?: string }): Promise<T>;

index.ts

  const transaction: Transaction = await json<Transaction>(req)

I tried send a PR to DefinitelyTyped, but I faced the following dtslint rule, no-unnecessary-generics.

https://github.com/Microsoft/dtslint/blob/master/docs/no-unnecessary-generics.md

Currently I created my d.ts file, so not a serious problem. However, I think it would be great if someone else can use generic type.

How do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants