I'm having these error messages when running a command with ts-node:

metaplex/js/packages/cli/src/candy-machine-v2-cli.ts:499:16 - error TS2569: Type 'Uint8Array' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

499             ...thisSlice.slice(4, 36).filter(n => n !== 0),
                   
metaplex/js/packages/cli/src/candy-machine-v2-cli.ts:502:16 - error TS2569: Type 'Uint8Array' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

502             ...thisSlice.slice(40, 240).filter(n => n !== 0),```

I've already tried updating tsconfig.json target to es5 and es2015 and also adding the downlevelIteration: true line, but the error keep the same.

What should I do?

1

There are 1 best solutions below

0
On

Note that the ts-node README is fairly exhaustive and covers even more than what I'll detailed here.

Because you haven't provided a minimal, reproducible example, I'll show you a self-contained example to demonstrate a working ESM project configuration using ts-node and the current LTS release of Node.js (v16.14.0 at the time I post this answer).

I'll show the contents of each file and detail the important configuration options in comments and/or after the contents:

./package.json:

{
  "name": "so-71450483",
  "version": "0.1.0",
  "description": "",
  "type": "module",
  "scripts": {
    "dev": "ts-node --esm src/main.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "@types/node": "^17.0.21",
    "ts-node": "^10.7.0",
    "typescript": "^4.6.2"
  }
}

  • devDependencies:

    • @types/node: needed for Node.js APIs
    • ts-node: part of your question
    • typescript: part of your question
  • type should be "module" for ESM

  • scripts.dev: I added this npm script to make running the project easier to type on the command line. The ts-node docs section about ES modules explains the requirements for enabling ESM support and shows using a CLI argument to run code in ESM mode: ts-node --esm src/main.ts.

    Now, to run your program with ts-node, you don't need to remember this syntax: you just need to use the command npm run dev.

./tsconfig.json:

{
  "compilerOptions": {
    // Node 16 ESM options
    // Ref: https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping#node-16
    "lib": ["es2021"],
    "module": "esnext",
    "moduleResolution": "node",
    "target": "es2021",

    // Suggestion: strict options for safety
    "strict": true,
    "exactOptionalPropertyTypes": true,
    "isolatedModules": true,
    "noUncheckedIndexedAccess": true,
    "useUnknownInCatchVariables": true,

    // ts-node overrides these options, but would be useful for actual compilation
    // "declaration": true,
    // "outDir": "dist",
  },
  "include": [
    "./src/**/*"
  ]
}

See comments above.

  • The only mandatory "compilerOptions" are the ones in the first section under "Node 16 ESM options".
  • Your source files will be in the ./src directory of your project.

Here's a link to the TSConfig reference: https://www.typescriptlang.org/tsconfig

./src/main.ts:

import {TextDecoder} from 'util';

let u8Array = new Uint8Array([85, 104, 104, 46, 46, 46, 32, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 63]);

u8Array = Uint8Array.from([72, ...u8Array.slice(8, -1)]);
//                             ^^^
// Iterating a Uint8Array using spread syntax,
// just like in the code shown in the error message in your question

const result = new TextDecoder().decode(u8Array);
console.log(result); // logs "Hello world"

There's the example code, which uses the same spread syntax on a Uint8Array, just like in your question.


Now, let's install everything and run it:

$ node --version
v16.14.0
$ npm --version
8.3.1
$ npm install

added 17 packages, and audited 18 packages in 4s

found 0 vulnerabilities
$ npm run dev

> [email protected] dev
> ts-node --esm src/main.ts

Hello world

Success: The output is as expected with no problems.