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

type check error with rpt2 but not tsc -- can't repro, Vite misconfiguration #305

Closed
binvb opened this issue Mar 28, 2022 · 3 comments
Closed
Labels
kind: support Asking for support with something or a specific use case problem: needs more info This issue needs more information in order to handle it problem: stale Issue has not been responded to in some time scope: integration Related to an integration, not necessarily to core (but could influence core) scope: Vite Related to integration with Vite, not Rollup, which this plugin was designed for solution: can't repro An attempt to reproduce has been tried and failed solution: invalid This doesn't seem right

Comments

@binvb
Copy link

binvb commented Mar 28, 2022

What happens and why it is wrong

rpt2 throw type error but tsc command was fine, any body help?

[rpt2] queue.ts(1,5): semantic error TS7034: Variable 'EventQueue' implicitly has type 'any[]' in some locations where its type cannot be determine
  interface Task {
    (): Promise<any>
  }
  let EventQueue: Task[] = []
  let pending: boolean = false

  function addQueue(event: Task):void {
      EventQueue.push(event)
      if(!pending) {
          executionQueue(event)
      }
  }

  async function executionQueue(event: Task): Promise<void> {
      pending = true
      await event()
      EventQueue.shift()
      pending = false
      if(EventQueue.length) {
          executionQueue(EventQueue[0])
      }
  }

  export default addQueue

Environment

macOS

Versions
 rollup-plugin-typescript2: ^0.31.2 => 0.31.2 
  typescript: ^4.6.3 => 4.6.3 

rollup.config.js

`rollup.config.js`:
const { resolve, basename, dirname } = require('path')
const vue = require('@vitejs/plugin-vue')
const { build } = require('vite')
const glob = require('fast-glob')
// const dts = require('vite-plugin-dts')
const typescript2 = require('rollup-plugin-typescript2')

function packPeers() {
  const imports = []

  const getImports = () => {
    const files = glob.sync([resolve(__dirname, '../packages/**/index.vue')], {objectMode: true}) // get all component

    files.forEach(file => {
      let _dir = dirname(file.path)
      let _basename = basename(_dir)

      imports.push({
        name: _basename, // get base path name
        entry: file.path,
        formats: ['es'],
      })
    })
  }
  
  getImports()
  
  imports.forEach(async item => {
    await build({
      outDir: 'dist',
      plugins: [
        vue(),
        typescript2()
      ],
      build: {
        lib: item,
        rollupOptions: {
          // 确保外部化处理那些你不想打包进库的依赖
          external: ['vue'],
          output: {
            // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
            globals: {
              vue: 'Vue'
            },
            assetFileNames: `${item.name}/[name].[ext]`,
            entryFileNames: () => `${item.name}/index.[format].js`
          }
        }
      }
    })
  })
}

packPeers()

tsconfig.json

`tsconfig.json`:
{
  "compilerOptions": {
    "target": "esnext",
    "useDefineForClassFields": true,
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "allowJs": true,
    "lib": ["esnext", "dom"],
    "skipLibCheck": true,
    "paths": {
      "packages/*": ["./packages/*"]
    }
  },
  "include": [
    "*.ts",
    "src/**/*.ts",
    "src/**/*.d.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "packages/**/*.ts",
    "packages/**/*.d.ts",
    "packages/**/*.tsx",
    "packages/**/*.vue"
  ]
}

package.json

`package.json`:
  "dependencies": {
    "@babel/core": "^7.16.0",
    "@babel/eslint-parser": "^7.16.0",
    "@types/lodash": "^4.14.176",
    "less": "^4.1.2",
    "lodash": "^4.17.21",
    "vue": "^3.2.16",
    "vue-eslint-parser": "^8.0.1"
  },
  "devDependencies": {
    "@rollup/plugin-babel": "^5.3.1",
    "@types/node": "^17.0.22",
    "@types/rollup-plugin-css-only": "^3.1.0",
    "@typescript-eslint/eslint-plugin": "^5.2.0",
    "@typescript-eslint/parser": "^5.2.0",
    "@vitejs/plugin-vue": "^1.9.3",
    "@vue/eslint-config-prettier": "^6.0.0",
    "@vue/eslint-config-typescript": "^8.0.0",
    "eslint": "^8.1.0",
    "eslint-config-alloy": "^4.4.0",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-vue": "^8.0.3",
    "fast-glob": "^3.2.11",
    "magic-string": "^0.25.7",
    "prettier": "^2.4.1",
    "rollup-plugin-dynamic-import-variables": "^1.1.0",
    "rollup-plugin-typescript2": "^0.31.2",
    "tslib": "^2.3.1",
    "typescript": "^4.6.3",
    "vite": "^2.8.6",
    "vite-plugin-dts": "^1.0.3",
    "vite-plugin-inspect": "^0.3.10",
    "vue-tsc": "^0.33.6"
  }

plugin output with verbosity 3

plugin output with verbosity 3:
@agilgur5
Copy link
Collaborator

If tsc was fine, there might be a TS version mismatch. Have you tried passing your TS dep through the typescript option?

@agilgur5 agilgur5 changed the title type check error type check error with rpt2 but not tsc Apr 23, 2022
@agilgur5 agilgur5 added problem: needs more info This issue needs more information in order to handle it problem: stale Issue has not been responded to in some time labels May 2, 2022
@agilgur5
Copy link
Collaborator

agilgur5 commented Jun 8, 2022

I created a minimal reproduction here and was unable to reproduce this error.

Both tsc and rpt2 give the same error in their latest versions (4.7.3 for TS, 0.32.1 for rpt2).
This error is different from the one you wrote though:

❯ npm run tsc
$ tsc
src/index.ts:23:20 - error TS2345: Argument of type 'Task | undefined' is not assignable to parameter of type 'Task'.
  Type 'undefined' is not assignable to type 'Task'.

23     executionQueue(EventQueue![0]);
                      ~~~~~~~~~~~~~~


Found 1 error in src/index.ts:23
❯ npm run build
$ rollup -c

./src/index.ts → ./dist/index.ts...
[!] (plugin rpt2) Error: /home/projects/rpt2-issue-305/src/index.ts(23,20): semantic error TS2345: Argument of type 'Task | undefined' is not assignable to parameter of type 'Task'.
  Type 'undefined' is not assignable to type 'Task'.
src/index.ts
Error: /home/projects/rpt2-issue-305/src/index.ts(23,20): semantic error TS2345: Argument of type 'Task | undefined' is not assignable to parameter of type 'Task'.
  Type 'undefined' is not assignable to type 'Task'.
    at error (/home/projects/rpt2-issue-305/node_modules/rollup/dist/shared/rollup.js:198:30)
    at throwPluginError (/home/projects/rpt2-issue-305/node_modules/rollup/dist/shared/rollup.js:21936:12)
    at Object.error (/home/projects/rpt2-issue-305/node_modules/rollup/dist/shared/rollup.js:22659:20)
    at Object.error (/home/projects/rpt2-issue-305/node_modules/rollup/dist/shared/rollup.js:22113:42)
    at RollupContext.error (/home/projects/rpt2-issue-305/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:20259:30)
    at eval (/home/projects/rpt2-issue-305/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:29527:23)
    at Array.forEach (<anonymous>)
    at printDiagnostics (/home/projects/rpt2-issue-305/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:29500:17)
    at Object.transform (/home/projects/rpt2-issue-305/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:29785:17)
    at eval (/home/projects/rpt2-issue-305/node_modules/rollup/dist/shared/rollup.js:22868:37)

So the error you're getting is likely due to a problem in your environment, and not a bug in rpt2.

The above error can be fixed by adding a non-null assertion operator (!) on that line: executionQueue(EventQueue[0]!). Both tsc and rpt2 will then pass.
The proper way to fix that, though, would be with a type-guard that ensures the type has at least one element.

@agilgur5 agilgur5 closed this as completed Jun 8, 2022
@agilgur5 agilgur5 added kind: support Asking for support with something or a specific use case solution: can't repro An attempt to reproduce has been tried and failed labels Jun 8, 2022
@agilgur5 agilgur5 changed the title type check error with rpt2 but not tsc type check error with rpt2 but not tsc -- can't repro Jun 9, 2022
@agilgur5 agilgur5 changed the title type check error with rpt2 but not tsc -- can't repro type check error with rpt2 but not tsc -- can't repro Jul 30, 2022
@agilgur5
Copy link
Collaborator

So you hadn't mentioned that you were using Vite, which is not Rollup, which I must've missed before when reading your config, as that is not a rollup.config.js, but a script that calls build from the Vite API.

You didn't use enforce: 'pre' to run rpt2 before esbuild, or otherwise disable esbuild's support for TS (e.g. add exclude: /\.tsx?$/ to the Vite esbuild config), so esbuild is compiling your TS, and then rpt2 is running on untyped JS, which can certainly result in lots of weird type-check errors given that it's untyped at this point.

So it seems like you have a misconfiguration here.

@agilgur5 agilgur5 added the solution: invalid This doesn't seem right label Jul 30, 2022
@agilgur5 agilgur5 changed the title type check error with rpt2 but not tsc -- can't repro type check error with rpt2 but not tsc -- can't repro, Vite misconfiguration Jul 30, 2022
@agilgur5 agilgur5 added the scope: integration Related to an integration, not necessarily to core (but could influence core) label Jul 30, 2022
@agilgur5 agilgur5 added the scope: Vite Related to integration with Vite, not Rollup, which this plugin was designed for label Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: support Asking for support with something or a specific use case problem: needs more info This issue needs more information in order to handle it problem: stale Issue has not been responded to in some time scope: integration Related to an integration, not necessarily to core (but could influence core) scope: Vite Related to integration with Vite, not Rollup, which this plugin was designed for solution: can't repro An attempt to reproduce has been tried and failed solution: invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants