`import()` type annotations are forbidden.

huangapple go评论83阅读模式
英文:

`import()` type annotations are forbidden

问题

export const machine = createMachine(
{
tsTypes: {} as import("./testMachine.server.typegen").Typegen0,
...,
}


然而,这个类型转换引发了以下错误:

import() 类型注释是被禁止的.eslint@typescript-eslint/consistent-type-imports
interface Typegen0


我研究了动态导入,但似乎无法解决这个问题:

const dynamicImport = async() => await import("./testMachine.server.typegen")


这些信息来自我的 eslint。

<details>
<summary>英文:</summary>

I am using XState on the backend and per the XState docs, I added type annotation to my state machine&#39;s config:

export const machine = createMachine(
{
tsTypes: {} as import("./testMachine.server.typegen").Typegen0,
...,
}

However, the type cast is throwing this error: 

import() type annotations are forbidden.eslint@typescript-eslint/consistent-type-imports
interface Typegen0


I looked into dynamic imports, but that doesn&#39;t seem to fix the issue:

const dynamicImport = async() => await import("./testMachine.server.typegen")


This is from my eslint.

</details>


# 答案1
**得分**: 1

这似乎只是一个 linting 错误。您的 eslint 配置期望以某种特定方式导入类型。我会假设类型推断和您的代码仍然可以正常工作。

您可以通过将忽略注释直接放在出现错误的行之上来禁用 linting 错误。我不确定我是否完全正确理解了注释,但朝这个方向的注释应该可以禁用错误。

```javascript
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
// @ts-ignore

否则,您也可以使用 这个答案 中的导入语法,或相应地调整您的 eslint 配置。

顺便说一句,我认为导入类型不需要使用 await

英文:

It seems like it is just a linting error. Your eslint config expects a certain way of importing types. I would assume the type inference and your code still works.

You can disable the linting error by putting an ignore comment directly above the line with the error. I am not sure if I got the comment 100% right, but sth in that direction should disable the error.

// eslint-disable-next-line @typescript-eslint/consistent-type-imports
// @ts-ignore

Otherwise, you can also use the import syntax of this answer or adjust your eslint config accordingly.

Btw, I assume await is never needed for importing types.

答案2

得分: 0

使用 import() 进行类型导入在默认情况下被 consistent-type-imports eslint 规则禁止,但你可以通过在你的 .eslintrc.json rules 部分将 disallowTypeAnnotations 设置为 false 来允许它:

{
  "rules": {
    "@typescript-eslint/consistent-type-imports": ["error", {
      "disallowTypeAnnotations": false
    }],
...
英文:

Using import() for typing is forbidden by default in the consistent-type-imports eslint rule, but you can just allow it by setting disallowTypeAnnotations to false in your .eslintrc.json rules section:

{
  &quot;rules&quot;: {
    &quot;@typescript-eslint/consistent-type-imports&quot;: [&quot;error&quot;, {
      &quot;disallowTypeAnnotations&quot;: false
    }],
...

答案3

得分: -1

以下是翻译好的部分:

It says you MUST import types as

import type { Typegen0 } from &quot;./testMachine.server.typegen&quot;
export const machine = createMachine(
    {
      tsTypes: {} as Typegen0,
    ...,
    }

You can read more in the documentation: https://typescript-eslint.io/rules/consistent-type-imports/#prefer

英文:

It says you MUST import types as

import type { Typegen0 } from &quot;./testMachine.server.typegen&quot;
export const machine = createMachine(
    {
      tsTypes: {} as Typegen0,
    ...,
    }

You can read more in the documentation: https://typescript-eslint.io/rules/consistent-type-imports/#prefer

huangapple
  • 本文由 发表于 2023年4月7日 01:20:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定