Ts-node: TS2322 when using async/await

Created on 1 Feb 2019  路  5Comments  路  Source: TypeStrong/ts-node

What is the current behaviour

When i am using an async function in my codebase like that :

export const testing = async () => {
  return "Hello world";
};

Then in my code :

testing();

After running :

ts-node index.ts

i get the following error :

error TS2322: Type '0' is not assignable to type '{ label: number; sent: () => any; trys: any[]; ops: any[]; }'.

Informations

// tsconfig.json 

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es5",                  
    "module": "commonjs",
    "lib": [
      "es2015",
      "es2017",
      "scripthost",
      "es2015.promise",
      "es2015.generator",
      "es2015.iterable",
      "dom",
      "dom.iterable"
    ],
    "resolveJsonModule": true,
    "plugins": [
      {
        "name": "typescript-tslint-plugin"
      }
    ],

    /* Strict Type-Checking Options */
    "strict": false,                          
    "noImplicitAny": false,                 
     "noImplicitThis": false,                
    "typeRoots": ["./typings"],                       
    "types": ["node", "jest"],                          
    "esModuleInterop": true                 
  }
}
// package.json 
{
  "devDependencies": {
    "@types/bunyan": "^1.8.5",
    "@types/jest": "^23.3.13",
    "@types/node": "^10.12.19",
    "@types/ramda": "^0.25.47",
    "ts-jest": "^23.10.5",
    "ts-node": "^8.0.2",
    "tslint": "^5.12.1",
    "tslint-config-prettier": "^1.17.0",
    "tslint-config-standard": "^8.0.1",
    "typescript": "^3.3.1"
  }
}
// index.ts
require("ts-node").register();
require("./src/index").start(process.env);

What is the expected behaviour

When i run ts-node index.ts and i am using async await notation in my codebase, the compiler should not throw errors.

needs more info

Most helpful comment

This error happens when you are compiling your .ts twice because you double-registered ts-node.

When an async function is compiled, it gets converted to use __awaiter and __generator helpers.
Playground example

Then, if you try to compile that output again as if it were TypeScript input, you get type errors inside the __generator helper function, because that function is raw JS and does not have the proper type annotations. It is not meant to be compiled as TypeScript.
On line 25, you get the OPs error: Type '0' is not assignable to type '{ label: number; sent: () => any; trys: any[]; ops: any[]; }'.
Playground example

Closing this as answered and not a ts-node bug. There are other issues that talk about improving reporting around accidental double-registration of ts-node if you're interested.

All 5 comments

It seems unlikely those two lines have caused this error. It appears there's more to this project that is causing that error. That sort of error looks like tuple assignment issues. I also can't see where that type comes from in your example or anything around 0. Can you share more for someone to be able to help you? Finally, does this compile with tsc?

We're having the same error here in Windows 10. It doesn't occur in macs or linux. So strange. Any update on this? The error is happening with async. When we removed the async functions, it ran.

Can you share any repro? I can test it and see if I can narrow down the issue. Also accepting any PRs to fix it, if it鈥檚 only affecting Windows it may be a path issue somewhere in the code base.

@blakeembrey we found here that the problem is when we used ts-node/register, with the most recent version of ts-node and nodemon.

Actually, when I updated the packages, even in my macbook the problem started to happen and not only in windows.

in package.json we were using nodemon -r ts-node/register. I believe the problem with kalutheo is because he's is using register:

// index.ts
require("ts-node").register();

to solve this problem with async/await, we used nodemon --exec ts-node and it worked. it follows the working scripts in package.json:

"dev": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' NODE_ENV=development nodemon --watch 'server/**/*.ts' --exec ts-node server/index.ts",
"win-dev": "SET TS_NODE_COMPILER_OPTIONS={\"module\":\"commonjs\"} && SET NODE_ENV=development&& nodemon --watch 'server/**/*.ts' -exec ts-node server/index.ts"

This error happens when you are compiling your .ts twice because you double-registered ts-node.

When an async function is compiled, it gets converted to use __awaiter and __generator helpers.
Playground example

Then, if you try to compile that output again as if it were TypeScript input, you get type errors inside the __generator helper function, because that function is raw JS and does not have the proper type annotations. It is not meant to be compiled as TypeScript.
On line 25, you get the OPs error: Type '0' is not assignable to type '{ label: number; sent: () => any; trys: any[]; ops: any[]; }'.
Playground example

Closing this as answered and not a ts-node bug. There are other issues that talk about improving reporting around accidental double-registration of ts-node if you're interested.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sanex3339 picture sanex3339  路  4Comments

Borewit picture Borewit  路  3Comments

joshua-tj picture joshua-tj  路  3Comments

conordickinson picture conordickinson  路  4Comments

watzon picture watzon  路  3Comments