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

Scoped style leads to error: TS7006: Parameter 'n' implicitly has an 'any' type #1915

Open
shameleo opened this issue Dec 29, 2021 · 27 comments

Comments

@shameleo
Copy link

Version

16.8.3, 17.0.0, maybe older

Reproduction link

github.com

Steps to reproduce

npm install
npm run dev

What is expected?

No errors

What is actually happening?

Error: TS7006: Parameter 'n' implicitly has an 'any' type


To get this error style must be scoped and template must access some component data

@chriamue
Copy link

chriamue commented Jan 1, 2022

"vue-loader": "16.5" works for me. "vue-loader": "16.6" failes:

[tsl] ERROR in /src/views/About.vue.ts(3,22)
TS7006: Parameter 'n' implicitly has an 'any' type.

@lexeek
Copy link

lexeek commented Jan 27, 2022

I have the same problem it occurs only if my components have the "scoped" attribute.
TS7006: Parameter 'n' implicitly has an 'any' type.

@stigmh
Copy link

stigmh commented Mar 25, 2022

Removing strict: true from tsconfig.json is a temporary bad workaround, perhaps it would work to allow usage of any instead. I however hope to see this resolved soon.

@henrikra
Copy link

henrikra commented Jul 5, 2022

Removing strict is very bad for the codebase.

Have you guys heard any news on this?

@CornerSyrup
Copy link

I got 4 components having the same problem.

Those failed to build with Webpack, but succeed in watch mode.
It first fail in watch mode, but I got succeed after removing scoped in one of them.
The best part is, it still work even I put it back...

@countMort
Copy link

Same problem here. I also get some other build time ts errors, resolving by changing strict to false.

ylysyym added a commit to ylysyym/coscheduler that referenced this issue Sep 16, 2022
@yuiidev
Copy link

yuiidev commented Sep 20, 2022

Issue still exists.

@entioentio
Copy link

entioentio commented Sep 22, 2022

I found this thread finally! Was trying to wrap my head around this issue in the span of the last couple of weeks.
My (somewhat silly) workaround is to disable the check when building (it doesn't fail when serving);

loader: 'ts-loader',
options: {
    ignoreDiagnostics: isWebpackServing ? [] : [7006]
}

This is far from ideal, but I catch most this kind of error during development, mostly in intelliSense.

@chriskulwik
Copy link

@entioentio where is this variable isWebpackServing defined?

@entioentio
Copy link

Sorry, true that. That's my local variable in webpack.l config const isWebpackServing = !!~process.argv.indexOf('serve'); but you can check either mode in which webpack is working or environment.

@muster-mark
Copy link

I get this errror even when the template is not accessing component data. It worked fine when the template was <div></div>, but I got the error with a child element - i.e. when changing it to <div><span></span></div>.
Using v17.

@pkly
Copy link

pkly commented Oct 25, 2022

I thought I was going insane since I couldn't see anything.
Very much an issue in 17, annoying for usage with Webpack Encore since it's <15 and >=17.

@sprout2000
Copy link

sprout2000 commented Nov 29, 2022

I could find a work-around that uses Babel, but maybe not the essential solution.

https://www.npmjs.com/package/babel-preset-typescript-vue3

npm i -D @babel/core @babel/preset-env @babel/preset-typescript babel-loader babel-preset-typescript-vue3

and webpack.config.ts:

  {
    test: /\.ts$/,
    exclude: /node_modules/,
    loader: 'babel-loader',
    options: {
      presets: [
        '@babel/preset-env',
        'babel-preset-typescript-vue3',
        '@babel/preset-typescript',
      ],
    },
  },

@rob-barber
Copy link

rob-barber commented Dec 19, 2022

This was happening to me. For me it wasn't due to the scoped style but rather from using typescript for my script. I removed the lang="ts" from my script tag of my Vue component and it went away.

Edit: Spoke too soon. The above initially worked for me until I added a mixin to the scoped CSS. Now the original issue is back.

@nspyke
Copy link

nspyke commented Dec 28, 2022

I hit this error as well. I'm using v17 with Symfony Webpack Encore.
As a workaround, removing scoped from the <style> tags fixed it for me, but I would like to add them back once this is fixed.

@sakotsu
Copy link

sakotsu commented Jan 14, 2023

I solved it this way instead of removing scoped.

tsconfig.json:

{
  "compilerOptions": {
    "noImplicitAny": false,
    ...
  }
}

@BR0kEN-
Copy link

BR0kEN- commented Jan 29, 2023

Greet me in the club.

The problem mysteriously does not appear in the watch mode (I'm using the Laravel Mix) so I came up with the idea to set the noImplicitAny TS compiler option to false for the non-watching Webpack modes.

Replace Mix.isWatching() with your logic for determining the watch mode.

// @todo Delete once https://github.com/vuejs/vue-loader/issues/1915 is resolved.
// @todo Don't forget to remove the issue mention from the README.md.
const tsOptions = Mix.isWatching()
  ? undefined
  : {
    compilerOptions: {
      noImplicitAny: false,
    },
  };

mix
  .ts('js/app.ts', 'js', tsOptions)
  .vue();

The Webpack rule would have looked this way:

{
  test: /\.tsx?$/,
  loader: 'ts-loader',
  options: {
    appendTsSuffixTo: [/\.vue$/],
    ...(Mix.isWatching() ? {} : { compilerOptions: { noImplicitAny: false } })
  },
}

@chriskulwik
Copy link

I'm still having this issue on version 17.1.1. Can someone please take a look at this? 🙏 @yyx990803 @sodatea

@dmfilipenko
Copy link

dmfilipenko commented Sep 27, 2023

Same here. All of these symptoms.
I'm trying to write the SFC component

<script lang="ts" setup>
//something
</script>
<template>
<div>
</div>
</template>
<style scoped>
</style>

Without scoped attribute everything works fine. Also without the setup attribute everything is also works fine.

@d9r-dev
Copy link

d9r-dev commented Oct 23, 2023

I stumbled upon this issue. The problem still exists. Only solution I could find is to remove the scoped attribute from the style tag.

@adpeyre
Copy link

adpeyre commented Jan 1, 2024

Same issue for me.
I also have more errors in prod mode compared to dev mode.

Example with a bad type for v-key in a vue component :

  • TS2322: Type '{ to: { name: string; }; label: string; }' is not assignable to type 'string | number | symbol | undefined'

This error is not detected in dev mode.

@brentswisher
Copy link

Just ran into this issue in the latest version too, turns out our organization has run into this pretty frequently lately 😞

I opted to add a targeted setting to set noImplicitAny to false only at build time as a workaround, so at least it flags in the IDE, but still doesn't feel great:

{
  test: /\.ts$/,
  exclude: /node_modules/,
  use: {
    loader: "ts-loader",
    options: {
      appendTsSuffixTo: [/\.vue$/],
      compilerOptions: {
        noImplicitAny: false, // Necessary until the following issue is fixed: https://github.com/vuejs/vue-loader/issues/1915
      },
    },
  },
},

What was really weird was if I ran it using webpack serve it would error, but if I re-saved the file it was erroring on without changing anything, the error went away and the project compiled successfully. However,webpack build consistently failed every time.

Feels like maybe some kind of a race condition?

@Threebow
Copy link

Threebow commented Feb 2, 2024

I am having the same issue. Initially, I downgraded to 16.5 as suggested by @chriamue, and that worked for me. However, in newer vue-loader versions, there have been numerous fixes made to remedy issues that occur as a result of using imported types as props (see vuejs/core#8482). In essence, I was having significant dificulty using this older version while trying to set up my types how I wanted to. Updating to the newest version, I now get only this Parameter 'n' implicitly has an 'any' type. error.

It seems that it may be an issue in Vue's SFC compiler: @xesxen made a pull request that seems like it would fix the cause of this here, I am not 100% sure without some input from them. Regardless, it would be amazing to see this issue addressed, especially since it is causing big issues for many people as evident in this thread, and seeing as people have made active attempts to fix it (i.e. the aforementioned PR) that have seem to been lost in the sea of issues and PRs.

Would it be possible to please have someone take a look at this after so long? @yyx990803 @sodatea 🙏🏻

@tehreet
Copy link

tehreet commented Feb 2, 2024

Also very interested to see the resolution to this issue.

@Threebow
Copy link

Threebow commented Feb 3, 2024

In essence, I was having significant dificulty using this older version while trying to set up my types how I wanted to.

To elaborate on this and show an example of the error I get instead of this one when downgrading to 16.5:

Module Error (from ./node_modules/.pnpm/vue-loader@16.5.0_webpack@5.89.0/node_modules/vue-loader/dist/templateLoader.js):
[@vue/compiler-sfc] No fs option provided to `compileScript` in non-Node environment. File system access is required for resolving imported types.

.\<redacted>\src\modules\Error.vue
5  |    import type { ErrorDTO } from "@/modules/Error.dto"
6  |  
7  |    defineProps<ErrorDTO>()
   |               ^^^^^^^^
8  |  </script>
9  |  

@/modules/Error.dto is a file Error.dto.ts that contains only the following few lines:

export type ErrorDTO = {
	status: number
	eventId?: string
	extra?: string
}

Initially, I tried to resolve this by making a fork of 16.5 and passing the fs option it requested as vue-loader doesn't export that option (see #2041, open for 9 months without a response).

I ended up getting it to work on occasion, but I was having a lot of really weird issues with imported types, and I could never get it working properly. Eventually I decided to give up on the fork idea and just tried to get it working with the newest version, but I experienced an issue with using intersections of imported types as props (see vuejs/core#8482).

It's incredibly frustrating that I've spent weeks on this and nothing works as expected. It always boils down to some arbitrary error that appears to come from vue-loader's generated *.vue.ts facade module. I would use Vite, but Vite does not natively support my use-case of backend bundling for SSR (manual server-side rendering with Koa, without an index.html file), so I cannot use it.

A warning for anyone coming here from Google in 2024:

If you are looking to use modern TypeScript within your Vue components or frontend code in general, and want to use vue-loader to integrate it within your webpack build, I strongly recommend you don't do so, either until these issues are fixed, or until vue-loader is finally deprecated and someone more tied into Webpack's ecosystem takes it upon themselves to maintain a better solution.

In my personal experience using many non-Vite bundlers, Vue+TS does not work as you'd expect in a lot of circumstances, unless you are using Vite alongside it. I am considering migrating away from Vue in my project for these reasons, as it seems the advent of Vite has led legacy modules that people still rely on (like vue-loader) to shy away from being supported.

Really hoping this changes, but unfortunately it seems that this is the way things are going for now.

@gde-pass
Copy link

Any news about this issue ? I'm not using script setup just because of that ...

@dvogiatzi
Copy link

@Threebow Thank you very much for the elaborate answer!

The issue still persists in May 2024. Any updates on this?

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

No branches or pull requests