最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Deno-ts typing support identifies lists (string[], ...) as empty object ({ }) - Stack Overflow

programmeradmin1浏览0评论

I work on VS Code and I use Deno. The deno.json in my project is quite simple:

{
    "compilerOptions": {
        "lib": [
            "DOM"
        ]
    },
    "imports": {
        "@std/path": "jsr:@std/path@^1.0.8"
    }
}

Same for the tsconfig.json:

{
  "compilerOptions": {
   
    "target": "ES2023",
    "module": "ES2022",
    "moduleResolution": "bundler",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

I'm dealing a deno-ts problem. To be more specific, the type checking of arrays doesn't seem to work at all.

I was working on a simple function whose purpose is to loop through a list of HTML pictures to link the <img> sources to the actual jpg/png. To make sure the query is correct, I check if the length of the list of elements is equal to the one of pictures:

function setPictures(authors: Author[]) {
    const imgList = document.querySelectorAll(".main__author__picture img")
    
    if (imgList.length !== authors.length) {
        console.error("lengths don't match !")
        return
    }
}

Here's the error due to authors.length:

Property 'length' does not exist on type '{}'.deno-ts(2339)

This is due to the fact that deno-ts doesn't understand what lists are:

(parameter) authors: {}

At first, I thought maybe this is due to the custom type Author. So I tried to write a simple array and let deno infer the type:

let test = ["yes", "no"]

However, the problem persists:

let test: {}

Enforcing the type does not fix it :

let test: string[]

I think it has to do with my configuration of Deno. Anyway, the code compiles because there is no syntax error here. It is an intellisense error.

The settings.json of my project (inside .vscode) is also very simple:

{
    "deno.enable": true,
    "deno.lint": true
}

I work on VS Code and I use Deno. The deno.json in my project is quite simple:

{
    "compilerOptions": {
        "lib": [
            "DOM"
        ]
    },
    "imports": {
        "@std/path": "jsr:@std/path@^1.0.8"
    }
}

Same for the tsconfig.json:

{
  "compilerOptions": {
   
    "target": "ES2023",
    "module": "ES2022",
    "moduleResolution": "bundler",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

I'm dealing a deno-ts problem. To be more specific, the type checking of arrays doesn't seem to work at all.

I was working on a simple function whose purpose is to loop through a list of HTML pictures to link the <img> sources to the actual jpg/png. To make sure the query is correct, I check if the length of the list of elements is equal to the one of pictures:

function setPictures(authors: Author[]) {
    const imgList = document.querySelectorAll(".main__author__picture img")
    
    if (imgList.length !== authors.length) {
        console.error("lengths don't match !")
        return
    }
}

Here's the error due to authors.length:

Property 'length' does not exist on type '{}'.deno-ts(2339)

This is due to the fact that deno-ts doesn't understand what lists are:

(parameter) authors: {}

At first, I thought maybe this is due to the custom type Author. So I tried to write a simple array and let deno infer the type:

let test = ["yes", "no"]

However, the problem persists:

let test: {}

Enforcing the type does not fix it :

let test: string[]

I think it has to do with my configuration of Deno. Anyway, the code compiles because there is no syntax error here. It is an intellisense error.

The settings.json of my project (inside .vscode) is also very simple:

{
    "deno.enable": true,
    "deno.lint": true
}
Share Improve this question edited Nov 20, 2024 at 6:29 Eli 618 bronze badges asked Nov 18, 2024 at 16:12 PehnnyPehnny 1291 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You can install these Node types deno add npm:@types/node

I found a solution in the official deno documentation. The project needs node types to type check lists like string[] correctly. I faced the same issue with types like Array<T>. However, declaring :

/// <reference types="npm:@types/node" />

At the head of the .mts file fixed everything.

发布评论

评论列表(0)

  1. 暂无评论