Deno: TS2345 [ERROR]: Argument of type 'BufReader' is not assignable to parameter of type 'string | BufReader'

Viewed 340

I'm exploring Deno with cvs files, but i got a problem with the parse. I'm thinking that the problem come with the old dependencies;

 deno info --unstable --no-check .\mod.ts

The majority are from @0.75 and I'm in the @1.5.
And I want to collaborate to help to solve problems in the Deno repo and not just downgrade to some stable version.

I'm using:

deno 1.5.0
v8 8.7.220.3
typescript 4.0.

I'm having having issue with this code:

import { join } from "https://deno.land/std/path/mod.ts";
import { parse } from "https://deno.land/std/encoding/csv.ts";
import { BufReader } from "https://deno.land/std/io/bufio.ts";

async function loadData() {
    const path = join(".", "data.csv");
  
    const file = await Deno.open(path);
    const bufReader = new BufReader(file);
    
    const result = await parse(bufReader, {
        skipFirstRow: true,
        comment: "#",
    });
    Deno.close(file.rid)
    console.log(result)
}

await loadData();

The error:

error: TS2345 [ERROR]: Argument of type 'BufReader' is not assignable to parameter of type 'string | BufReader'.
Type 'import("https://deno.land/std/io/bufio.ts").BufReader' is not assignable to 
type 'import("https://deno.land/std@0.75.0/io/bufio.ts").BufReader'.
    Types have separate declarations of a private property 'buf'.
  const result = await parse(bufReader, {
                             ~~~~~~~~~
    at...
3 Answers

In Deno @1.0.0 works fine

deno upgrade --version 1.0.0

And in @1.5.1 works fine too

deno upgrade --version 1.5.1

1.5.0 had many problems due to a change in the nature of the compiler. I suggest downgrading to a lower version or ultra to 1.5.1 to receive the fixes implemented later on.

You should import

import { BufReader } from "https://deno.land/std@0.75.0/io/bufio.ts";

instead of

import { BufReader } from "https://deno.land/std/io/bufio.ts";

Better approach:

But actual best practice in case of deno is to use deps.ts file for all your dependencies.

Create file deps.ts in the root directory and export your dependencies there.

// deps.ts

// Standard library dependencies
export { BufReader } from "https://deno.land/std@0.75.0/io/bufio.ts";

// Third party dependencies
// ...

and in your other files you should import from deps.ts. For example, if you had mod.ts file in the root directory:

import { BufReader } from "./deps.ts";
// ...

Good thing about this approach is that whenever you want to change version of your imported libraries you will have to change in one place instead of many.

Related