添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

We had to update our tsconfig file and this resulted in a new typescript error when creating a new SchemaBuilder.

Code:

export const builder = new SchemaBuilder<{}>(builderOptions);

Error:

error TS2351: This expression is not constructable.
Type 'typeof import("C:/Users/[...]/node_modules/@pothos/core/dts/index")' has no construct signatures.

Changes in our tsconfig:

  • Changed "module": "commonjs" to "module": "NodeNext"
  • Added line "moduleResolution": "NodeNext"
  • Full tsconfig.json:

    "compilerOptions": { "target": "ESNext", "module": "NodeNext", "allowJs": true, "declaration": false, "outDir": "built", "maxNodeModuleJsDepth": 10, "strict": true, "moduleResolution": "NodeNext", "allowSyntheticDefaultImports": true, "esModuleInterop": true, "skipLibCheck": true "include": ["src/**/*"], "exclude": ["node_modules"]

    Package versions:

    "@pothos/core": "^3.20.0"
    "typescript": "^4.8.2"
    

    Repository with error reproduction:
    Repository: builder-ts-error-reproduction

    Thanks for the report and reproduction!

    I spent some time looking at this today, and I am very unclear on how this is supposed to work for packages supporting both esm and cjs.

    Adding "type": "module" to package.json in pothos lets ts load the types correctly, but would completely break cjs support in pothos.

    It also looks like if I duplicate type declaration into the esm directory and remove "types" "exports.types" from package.json things would also work.

    This is a potential path forward, but would require 3 copies of the type definitions to be shipped. I kinda suspect this might be a bug in typescript where they are not properly account for the combination of "nodeNext" with packages that define "exports" for both cjs and esm.

    I won't have much time until late next week to dig into this more. If you have ideas on a path forward I am happy to help implement, but I don't really have a good enough understanding of how all the different config options interact to know how to proceed at the moment.

    After playing around with the error repoduction repo I succeed to make it works by changing this files :

    src/index.mts

    import { default as SchemaBuilder } from '@pothos/core'; // was import SchemaBuilder from "@pothos/core";
    const builderOptions = {
      plugins: [],
    export const builder = new SchemaBuilder<{}>(builderOptions);

    tsconfig.json

    "compilerOptions": { "sourceMap": true, "outDir": "dist", "rootDir": "src", "target": "es2021", "module": "node16", "strict": true, "lib": ["es2021"], "esModuleInterop": true, "skipLibCheck": true "exclude": ["dist"]

    BUT it only works up to Typescript version 4.8.2 ! If you install version 4.8.3 you will get

    src/index.mts:7:28 - error TS2351: This expression is not constructable.
      Type 'typeof import("C:/Users/mboidin/Documents/Playgrounds/builder-ts-error-reproduction/node_modules/@pothos/core/dts/index")' has no construct signatures.
    7 export const builder = new SchemaBuilder<{}>(builderOptions);
              

    relevant typescript issue: microsoft/TypeScript#50762

    Did some debugging, and it looks like the issue is that in nodeNext mode pothos at its root is a "cjs" package, and the "dts" files inside are treated as cjs in compatability mode. This makes ts think that the default export is an object with a "default" property, rather than being the builder class itself.

    Hum, now it's not working anymore :/

    TSError: ⨯ Unable to compile TypeScript:
    src/builder.mts:1:27 - error TS7016: Could not find a declaration file for module '@pothos/core'. 'C:/Users/mboidin/Documents/Projets/woozgo-backoffice-server/node_modules/@pothos/core/esm/index.js' implicitly has an 'any' type.
    Try npm i --save-dev @types/pothos__core if it exists or add a new declaration (.d.ts) file containing declare module '@pothos/core';

    1 import SchemaBuilder from '@pothos/core'