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 TS2742 error when upgrading 4.X.X to 5.4.0 #1053

Open
AGallouin opened this issue Jan 24, 2023 · 8 comments
Open

Typescript TS2742 error when upgrading 4.X.X to 5.4.0 #1053

AGallouin opened this issue Jan 24, 2023 · 8 comments
Labels

Comments

@AGallouin
Copy link

Hello,

When bumping neo4j-driver package from 4.4.2 to 5.4.0, I'm having issues with the base types of neo4j (Integer, Record, Session etc.).
All my neo4j connections are made using a facade package around neo4j-driver, and exposing basic methods and aliases to the types mentioned above to the rest of the codebase. Everytime typescript needs to infer a return type that includes one of thoses types, I have an error like this one.

(!) Plugin typescript: @rollup/plugin-typescript : The inferred type of 'int' cannot be named without a reference to 'neo4j-driver/node_modules/neo4j-driver-core/types/integer'. This is likely not portable. A type annotation is necessary.

The only simple solution i found until now is to add directly the package neo4j-driver-core as a dependency of my package even though the readme explicitly tells me otherwise.

Dependency schema if unclear:

Codebase ==> My neo4j driver package Wrapper ==> neo4j-driver ==> neo4j-driver-core
             ^ Exposes some methods with         ^ exposes
             base neo4j-driver base types        underlying
                                                 neo4j-driver-core
                                                 types

I don't even understand why does this error means exactly, do you have any insights on it ?

@AGallouin AGallouin added the bug label Jan 24, 2023
@bigmontz
Copy link
Contributor

bigmontz commented Jan 30, 2023

Hey @AGallouin, how do the package is being imported? which typescript version are you using? Could you check if you have the same issue with 5.3.0?

@bigmontz
Copy link
Contributor

Issue closed due inactivity. Please, open again the issue if the error persists and you have more evidences.

@dylanjt
Copy link

dylanjt commented Oct 27, 2023

@bigmontz what timing haha - I am looking at this issue right now as I am also facing this error.

@dylanjt
Copy link

dylanjt commented Oct 27, 2023

@bigmontz for me I've reproduced the issue on 5.14, 5.13, and 5.3.0 since it's mentioned above. I am using [email protected] in a pnpm workspace. My usage is like so:

// neo4j.ts
import neo4j from "neo4j-driver";

const { NEO4J_URI, NEO4J_AUTH_USERNAME, NEO4J_AUTH_PASSWORD } = process.env;

if (!NEO4J_URI || !NEO4J_AUTH_USERNAME || !NEO4J_AUTH_PASSWORD) {
  throw Error("Missing Neo4j environment variables");
}

export default neo4j.driver(
  NEO4J_URI,
  neo4j.auth.basic(NEO4J_AUTH_USERNAME, NEO4J_AUTH_PASSWORD)
);

// index.ts
import neo4j from "./neo4j";
import { Draft, Node, Relationship } from "./types";

export default neo4j;

export async function create<T extends keyof Node>(
  label: T,
  properties: Draft<Node[T]>
) {
  const session = neo4j.session();
  const res = await session.run<Node[T]>(
    `...Cypher query...`,
    { label, ...properties }
  );

  return res;
}

The declaration of the create function triggers the following error:

The inferred type of 'create' cannot be named without a reference to '.pnpm/[email protected]/node_modules/neo4j-driver-core/types/result'. This is likely not portable. A type annotation is necessary.

@dylanjt
Copy link

dylanjt commented Oct 27, 2023

For anyone in need of an interim solution: it's regrettable, but explicitly installing neo4j-driver-core and adding the following import resolves the issue for me:

import "neo4j-driver-core";

@bigmontz
Copy link
Contributor

Can you share your tsconfig file?

@bigmontz bigmontz reopened this Oct 27, 2023
@bigmontz
Copy link
Contributor

I had configured a project like this, npm run build works fine.

// tsconfig.json
{
  "compilerOptions": {
    "target": "ES5",
    "lib": ["ES6", "es2015"],
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "strictNullChecks": true,
    "esModuleInterop": true,
    "moduleResolution": "node",
    "downlevelIteration": true,
    "outDir": "lib",
    "declaration": true,
    "declarationDir": "types",
    "isolatedModules": true,
    "types": ["node"]
  },
  "include": [
    "index.ts"
  ]
}
// package.json
{
  "name": "github1053",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc -p tsconfig.json"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "neo4j-driver": "^5.14.0"
  },
  "devDependencies": {
    "@types/node": "^20.8.9",
    "typescript": "^5.2.2"
  }
}
// index.ts
import { RecordShape } from "neo4j-driver";
import neo4j from "./neo4j";

export default neo4j;

export async function create<Shape extends RecordShape = RecordShape>(
  label: string,
  properties: any
) {
  const session = neo4j.session();
  const res = await session.run<Shape>(
    `...Cypher query...`,
    { label, ...properties }
  );

  return res;
}
// neo4j.ts

import neo4j from "neo4j-driver";

// @ts-ignore
const { NEO4J_URI, NEO4J_AUTH_USERNAME, NEO4J_AUTH_PASSWORD } = process.env;

if (!NEO4J_URI || !NEO4J_AUTH_USERNAME || !NEO4J_AUTH_PASSWORD) {
  throw Error("Missing Neo4j environment variables");
}

export default neo4j.driver(
  NEO4J_URI,
  neo4j.auth.basic(NEO4J_AUTH_USERNAME, NEO4J_AUTH_PASSWORD)
);

@bigmontz
Copy link
Contributor

@dylanjt Can you share your tsconfig?

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

No branches or pull requests

3 participants