parcel-bundler / parcel

The zero configuration build tool for the web. πŸ“¦πŸš€
https://parceljs.org
MIT License
43.49k stars 2.27k forks source link

[bug] failed to parse "this" in TypeScript's type #8703

Open TechQuery opened 1 year ago

TechQuery commented 1 year ago

πŸŽ› Configuration

package.json

{
    "source": "source/index.ts",
    "types": "dist/index.d.ts",
    "module": "dist/index.esm.js",
    "main": "dist/index.js",
    "dependencies": {
        "@swc/helpers": "^0.4.14",
        "mobx-restful": "^0.6.0-rc.21",
        "qs": "^6.11.0",
        "regenerator-runtime": "^0.13.11",
        "web-utility": "^4.0.0"
    },
    "peerDependencies": {
        "mobx": ">=4 <6"
    },
    "devDependencies": {
        "@types/qs": "^6.9.7",
        "mobx": "^5.15.7",
        "parcel": "~2.6.2"
    },
    "browserslist": "> 0.5%, last 2 versions, not dead, IE 11",
    "targets": {
        "main": {
            "optimize": true
        }
    },
    "scripts": {
        "build": "rm -rf dist/  &&  parcel build",
        "prepublishOnly": "npm run build"
    }
}

tsconfig.json

{
    "compilerOptions": {
        "target": "ES5",
        "module": "ES6",
        "moduleResolution": "Node",
        "experimentalDecorators": true,
        "lib": ["ES2022", "DOM"]
    },
    "include": ["source/*.ts"]
}

πŸ€” Expected Behavior

Compile TypeScript source code as shown below successfully:

class Example {
    someKey = '';

    method() {
        const data = {} as Record<string, this['someKey']>;

        return data;
    }
}

😯 Current Behavior

🚨 Build failed.

@parcel/transformer-typescript-types: Got unexpected undefined

  Error: Got unexpected undefined
      at nullthrows 
  (D:\Work\idea2app\MobX-RESTful\node_modules\.pnpm\nullthrows@1.1.1\node_modules\nullthrows\nullthrows.js:7:15)
      at Object.transform 
  (D:\Work\idea2app\MobX-RESTful\node_modules\.pnpm\@parcel+transformer-typescript-types@2.6.2_typescript@4.7.4\node_modules\@parcel\transformer-typescript-types\lib\TSTypesTransformer.js:209:42)
      at Transformation.runTransformer 
  (D:\Work\idea2app\MobX-RESTful\wrapper\Strapi\node_modules\.pnpm\@parcel+core@2.6.2\node_modules\@parcel\core\lib\Transformation.js:653:23)
      at Transformation.runPipeline 
  (D:\Work\idea2app\MobX-RESTful\wrapper\Strapi\node_modules\.pnpm\@parcel+core@2.6.2\node_modules\@parcel\core\lib\Transformation.js:402:47)
      at Transformation.runPipelines 
  (D:\Work\idea2app\MobX-RESTful\wrapper\Strapi\node_modules\.pnpm\@parcel+core@2.6.2\node_modules\@parcel\core\lib\Transformation.js:265:51)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async Transformation.run 
  (D:\Work\idea2app\MobX-RESTful\wrapper\Strapi\node_modules\.pnpm\@parcel+core@2.6.2\node_modules\@parcel\core\lib\Transformation.js:183:21)
      at async Child.handleRequest
  (D:\Work\idea2app\MobX-RESTful\wrapper\Strapi\node_modules\.pnpm\@parcel+workers@2.6.2_@parcel+core@2.6.2\node_modules\@parcel\workers\lib\child.js:217:9)

πŸ’ Possible Solution

If I replace this with the class, this error disappeared:

class Example {
    someKey = '';

    method() {
-       const data = {} as Record<string, this['someKey']>;
+       const data = {} as Record<string, Example['someKey']>;

        return data;
    }
}

πŸ”¦ Context

Real world code:

https://github.com/idea2app/MobX-RESTful/pull/9/commits/0370678fd2592094c0225662303a4dd2510c20fd#diff-780bc523b57f373918475f4b3d1d120f938678b33c03bf79ce4f7b20b7085254R63-R69

🌍 Your Environment

Software Version(s)
Parcel 2.6.2
Node 16.18.0
PNPM 7.18.0
Operating System Windows 10 Pro 21H2
mischnic commented 1 year ago

Does it also still fail with Parcel 2.8.2?

TechQuery commented 1 year ago

Does it also still fail with Parcel 2.8.2?

  1. Same error in Parcel 2.8.2 & TypeScript 4.9.4
  2. Same error with one more warning in Parcel 2.8.2 & TypeScript 4.7.4:
@parcel/transformer-typescript-types: The inferred type of 'searchFilter' references an inaccessible 'this' type. A type   
annotation is necessary.

but TypeScript in VS Code told me nothing wrong.

mischnic commented 1 year ago

It's working for me: https://github.com/mischnic/parcel-issue-8703

TechQuery commented 1 year ago

It's working for me: https://github.com/mischnic/parcel-issue-8703

Could you test a more complex case? such as the context I shown above: https://github.com/idea2app/MobX-RESTful/commit/0370678fd2592094c0225662303a4dd2510c20fd#diff-780bc523b57f373918475f4b3d1d120f938678b33c03bf79ce4f7b20b7085254R63-R69

mischnic commented 1 year ago
$ git clone https://github.com/idea2app/MobX-RESTful
$ pnpm install
$ pnpm exec parcel build
✨ Built in 2.18s

dist/index.js         14.1 KB    609ms
dist/index.esm.js    18.07 KB     88ms

(And this is exactly the reason why I wanted you to prepare some isolated reproduction...)

TechQuery commented 1 year ago

It's working for me: https://github.com/mischnic/parcel-issue-8703

Could you test a more complex case? such as the context I shown above: https://github.com/idea2app/MobX-RESTful/commit/0370678fd2592094c0225662303a4dd2510c20fd#diff-780bc523b57f373918475f4b3d1d120f938678b33c03bf79ce4f7b20b7085254R63-R69

@mischnic look here, it's in a sub package, the permanent link directs to the error lines.

mischnic commented 1 year ago
$ git clone https://github.com/idea2app/MobX-RESTful
$ pnpm install
$ cd wrapper/Strap
$ pnpm install
$ pnpm build
> mobx-strapi@0.4.0-rc.6 build wrapper/Strapi
> rm -rf dist/  &&  parcel build

@parcel/transformer-typescript-types: Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier.

  wrapper/Strapi/node_modules/.pnpm/koajax@0.8.3/node_modules/koajax/dist/index.d.ts:22:1
    21 | };
  > 22 | enum FileMethod {
  >    | ^^^^^ Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier.
    23 |     text = "readAsText",
    24 |     dataURL = "readAsDataURL",

✨ Built in 5.40s

dist/index.js        3.24 KB    245ms
dist/index.esm.js    3.68 KB     68ms
dist/index.d.ts      2.32 KB     98ms
TechQuery commented 1 year ago

@mischnic To replace the class name with this can reproduce: https://github.com/idea2app/MobX-RESTful/blob/bb127f4e22e7fdeedea5cc9aa2bfc1295fc6a988/wrapper/Strapi/source/index.ts#L64

Use this branch: https://github.com/idea2app/MobX-RESTful/tree/update-bundler

TechQuery commented 1 year ago

Any update?

TechQuery commented 9 months ago

Does it also still fail with Parcel 2.8.2?

  1. Same error in Parcel 2.8.2 & TypeScript 4.9.4
  2. Same error with one more warning in Parcel 2.8.2 & TypeScript 4.7.4:
@parcel/transformer-typescript-types: The inferred type of 'searchFilter' references an inaccessible 'this' type. A type   
annotation is necessary.

but TypeScript in VS Code told me nothing wrong.

parcel@2.11.0 & typescript@5.3.3 throw the same error with the same code: https://github.com/idea2app/MobX-RESTful/blob/a450bafecd531d91f2a306a47bf763dbf5e6b12a/wrapper/Strapi/source/index.ts#L71

TechQuery commented 3 months ago

Does it also still fail with Parcel 2.8.2?

  1. Same error in Parcel 2.8.2 & TypeScript 4.9.4
  2. Same error with one more warning in Parcel 2.8.2 & TypeScript 4.7.4:
@parcel/transformer-typescript-types: The inferred type of 'searchFilter' references an inaccessible 'this' type. A type   
annotation is necessary.

but TypeScript in VS Code told me nothing wrong.

parcel@2.11.0 & typescript@5.3.3 throw the same error with the same code: https://github.com/idea2app/MobX-RESTful/blob/a450bafecd531d91f2a306a47bf763dbf5e6b12a/wrapper/Strapi/source/index.ts#L71

@mischnic This bug disappears with the latest parcel@2.12.0 & typescript@5.5.4 finally πŸŽ‰

TechQuery commented 2 months ago
  1. Same error in Parcel 2.8.2 & TypeScript 4.9.4
  2. Same error with one more warning in Parcel 2.8.2 & TypeScript 4.7.4:
@parcel/transformer-typescript-types: The inferred type of 'searchFilter' references an inaccessible 'this' type. A type   
annotation is necessary.

but TypeScript in VS Code told me nothing wrong.

parcel@2.11.0 & typescript@5.3.3 throw the same error with the same code: https://github.com/idea2app/MobX-RESTful/blob/a450bafecd531d91f2a306a47bf763dbf5e6b12a/wrapper/Strapi/source/index.ts#L71

@mischnic This bug disappears with the latest parcel@2.12.0 & typescript@5.5.4 finally πŸŽ‰

Unfortunately, this error comes out with the same verisons... https://github.com/idea2app/MobX-RESTful/actions/runs/10687983037/job/29626792910#step:7:51

The source code is here: https://github.com/idea2app/MobX-RESTful/pull/16/files#diff-780bc523b57f373918475f4b3d1d120f938678b33c03bf79ce4f7b20b7085254