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

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

Open
TechQuery opened this issue Dec 18, 2022 · 13 comments
Open

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

TechQuery opened this issue Dec 18, 2022 · 13 comments
Labels
Stale Inactive issues TypeScript

Comments

@TechQuery
Copy link

TechQuery commented Dec 18, 2022

🎛 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\[email protected]\node_modules\nullthrows\nullthrows.js:7:15)
      at Object.transform 
  (D:\Work\idea2app\MobX-RESTful\node_modules\.pnpm\@[email protected][email protected]\node_modules\@parcel\transformer-typescript-types\lib\TSTypesTransformer.js:209:42)
      at Transformation.runTransformer 
  (D:\Work\idea2app\MobX-RESTful\wrapper\Strapi\node_modules\.pnpm\@[email protected]\node_modules\@parcel\core\lib\Transformation.js:653:23)
      at Transformation.runPipeline 
  (D:\Work\idea2app\MobX-RESTful\wrapper\Strapi\node_modules\.pnpm\@[email protected]\node_modules\@parcel\core\lib\Transformation.js:402:47)
      at Transformation.runPipelines 
  (D:\Work\idea2app\MobX-RESTful\wrapper\Strapi\node_modules\.pnpm\@[email protected]\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\@[email protected]\node_modules\@parcel\core\lib\Transformation.js:183:21)
      at async Child.handleRequest
  (D:\Work\idea2app\MobX-RESTful\wrapper\Strapi\node_modules\.pnpm\@[email protected]_@[email protected]\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:

idea2app/MobX-RESTful@0370678#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
Copy link
Member

Does it also still fail with Parcel 2.8.2?

@TechQuery
Copy link
Author

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
Copy link
Member

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

@TechQuery
Copy link
Author

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:
idea2app/MobX-RESTful@0370678#diff-780bc523b57f373918475f4b3d1d120f938678b33c03bf79ce4f7b20b7085254R63-R69

@mischnic
Copy link
Member

$ 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
Copy link
Author

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:
idea2app/MobX-RESTful@0370678#diff-780bc523b57f373918475f4b3d1d120f938678b33c03bf79ce4f7b20b7085254R63-R69

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

@mischnic
Copy link
Member

$ git clone https://github.com/idea2app/MobX-RESTful
$ pnpm install
$ cd wrapper/Strap
$ pnpm install
$ pnpm build
> [email protected] 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/[email protected]/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
Copy link
Author

@github-actions github-actions bot added the Stale Inactive issues label Jul 19, 2023
@TechQuery
Copy link
Author

Any update?

@github-actions github-actions bot removed the Stale Inactive issues label Jul 19, 2023
@github-actions github-actions bot added the Stale Inactive issues label Jan 16, 2024
@TechQuery
Copy link
Author

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.

[email protected] & [email protected] throw the same error with the same code:
https://github.com/idea2app/MobX-RESTful/blob/a450bafecd531d91f2a306a47bf763dbf5e6b12a/wrapper/Strapi/source/index.ts#L71

@github-actions github-actions bot removed the Stale Inactive issues label Jan 29, 2024
@github-actions github-actions bot added the Stale Inactive issues label Jul 27, 2024
@TechQuery
Copy link
Author

TechQuery commented Jul 28, 2024

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.

[email protected] & [email protected] 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 [email protected] & [email protected] finally 🎉

@github-actions github-actions bot removed the Stale Inactive issues label Jul 28, 2024
@TechQuery
Copy link
Author

TechQuery commented Sep 3, 2024

  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.

[email protected] & [email protected] 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 [email protected] & [email protected] 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

@TechQuery TechQuery reopened this Sep 3, 2024
Copy link

github-actions bot commented Mar 3, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

@github-actions github-actions bot added the Stale Inactive issues label Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale Inactive issues TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants