Next.js: [canary] built-in type declarations for `next()` missing

Created on 7 Jun 2019  路  6Comments  路  Source: vercel/next.js

Bug report

Describe the bug

Encountered this bug while trying to upgrade the custom-server-typescript example to the latest canary version of Next.js.

When I try to initiate a server in my custom TypeScript server, the built-in TypeScript declarations doesn't seem to include the exported next() function used to initiate a server.

Before, initiating a next server works like this:

// ./server/index.ts

import * as next from 'next'

// ...
const app = next({ dev })

However, when I try to upgrade to canary and do the same, calling next() gives me this error:

// ./server/index.ts

import * as next from 'next'

// ...
const app = next({ dev }) // Error!
Cannot invoke an expression whose type lacks a call signature. Type 'typeof import("<snip>/node_modules/next/types/index")' has no compatible call signatures.

See "Additional context" for more information. I'll update this issue as I see more possible fixes.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Upgrade the next package in the mentioned example to latest canary version.
  2. Remove the old @types/next declarations.
  3. Try to import next and initiate a server.
  4. See error.

Expected behavior

  • No type errors when importing the default next module in a TypeScript custom server.
  • Base next module should be typed properly (see declarations on DefinitelyTyped for reference)

Screenshots

Using 8.1.0 + DefinitelyTyped typings:

image

Using canary:

image

System information

  • OS: macOS 10.14.5
  • Browser (if applies): Chrome
  • Version of Next.js: v8.1.1-canary.47

Additional context

It seems like the type declarations passed into package.json (types/index.d.ts) only includes the next/next-server helper types. However, it doesn't actually include the actual declaration of the next() function that exists on the main file (server/next.js).

Well, we seem to have the declarations for the main file over here, but since we have the types key set in package.json it's ignored in favour of the declaration file without the next() declaration!

It's also worth noting that if I import next/dist/server/next directly, the types work as normal.

See declarations on DefinitelyTyped for reference.

Most helpful comment

Looks like https://github.com/zeit/next.js/pull/7726 will fix this.

All 6 comments

@huv1k @Timer Hey! This is still an issue and can be reproduced with the steps given by @resir014.

Having the same issue. any solution so far? I had to downgrade to version next@^8.1.0 to make it work.

@Msordet @lomse I think the fixes made in https://github.com/zeit/next.js/pull/7547 only added some more additional types to the next.d.ts module, but didn't actually fix the actual issue that the types in index.d.ts (that doesn't include the types for next.d.ts) still takes precedence since it's defined on the types key on package.json.

I'll try to figure something out tomorrow.

Turns out I forgot to add this info in the first post. It's also worth noting that if I import next/dist/server/next directly, the types work as normal.

thanks for your investigation @resir014 , can confirm that import next from 'next/dist/server/next' fixes the problem

Looks like https://github.com/zeit/next.js/pull/7726 will fix this.

Was this page helpful?
0 / 5 - 0 ratings