TypeScript .entries() does not exist on type 'DOMTokenList'

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

TypeScript .entries() does not exist on type 'DOMTokenList'

问题

我正在使用vue3,vite和ts。我对DOMTokenList有一个问题。我想使用.entries()方法,但ts说属性 'entries' 在类型 'DOMTokenList' 上不存在

这是我的tsconfig文件。

{
  "extends": "@vue/tsconfig/tsconfig.web.json",
  "include": ["env.d.ts", "src/**/*", "src/**/*.vue", "main.d.ts"],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    },
    "typeRoots": ["./node_modules/@types", "src/core/types"],
    "ignoreDeprecations": "5.0",
    "lib":["DOM","ESNext"]
  },
  "references": [
    {
      "path": "./tsconfig.node.json"
    }
  ]
}

此外,我正在使用.enties()函数来扩展DOMTokenList。

DOMTokenList.prototype.removeStartWith = function(prefix:string){
    this.remove(...Array.from(this.entries()).map(([,c]) => c).filter(c => c.startsWith(prefix)))
}

我尝试了之前的ECMA(ES2019)版本,但没有得到任何结果。我该如何解决这个问题?谢谢建议。

英文:

I am using vue3, vite and ts. I have a porblem with DOMTokenList. I want to use .entries() method but ts says Property 'entries' does not exist on type 'DOMTokenList'

This is my tsconfig file

{
  "extends": "@vue/tsconfig/tsconfig.web.json",
  "include": ["env.d.ts", "src/**/*", "src/**/*.vue", "main.d.ts"],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    },
    "typeRoots": ["./node_modules/@types", "src/core/types"],
    "ignoreDeprecations": "5.0",
    "lib":["DOM","ESNext"]
  },
  "references": [
    {
      "path": "./tsconfig.node.json"
    }
  ]
}

Also I am using .enties() function to extend DOMTokenList

DOMTokenList.prototype.removeStartWith = function(prefix:string){
    this.remove(...Array.from(this.entries()).map(([,c]) => c).filter(c => c.startsWith(prefix)))
}

I tried previous ECMA (ES2019) versions but I didn't get any result. How can I solve that? Thanks for suggestions.

答案1

得分: 1

根据 @jcalz 的评论,我添加了 "DOM.Iterable" 库。问题已解决。我曾认为 "DOM" 库包含 "Iterable",但事实并非如此。

{
  "extends": "@vue/tsconfig/tsconfig.web.json",
  "include": ["env.d.ts", "src/**/*", "src/**/*.vue", "main.d.ts"],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/": ["./src/*"]
    },
    "typeRoots": ["./node_modules/@types", "src/core/types"],
    "ignoreDeprecations": "5.0",
    "lib": ["DOM", "DOM.Iterable", "ESNext"]
  },
  "references": [
    {
      "path": "./tsconfig.node.json"
    }
  ]
}
英文:

Based on @jcalz comment, I added the "DOM.Iterable" lib. And problem solved. I was thinking that the "DOM" lib contains "Iterable". But it didn't contain.

{
  "extends": "@vue/tsconfig/tsconfig.web.json",
  "include": ["env.d.ts", "src/**/*", "src/**/*.vue", "main.d.ts"],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    },
    "typeRoots": ["./node_modules/@types", "src/core/types"],
    "ignoreDeprecations": "5.0",
    "lib":["DOM","DOM.Iterable","ESNext"]
  },
  "references": [
    {
      "path": "./tsconfig.node.json"
    }
  ]
}

huangapple
  • 本文由 发表于 2023年5月21日 06:09:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297533.html
匿名

发表评论

匿名网友

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

确定