K6 global 'console' declaration clashes with the node 'console' types

In a project that has both k6 scripts and a TypeScript app you need both ‘k6’ and ‘node’ type packages. Unfortunately, because k6 declares the ‘console’ type in its global.d.ts file:

This clashes with the ‘console’ type in the ‘node’ type definition and you end up with this error when you try to build your app:

node_modules/@types/k6/global.d.ts:69:9 - error TS2451: Cannot redeclare block-scoped variable 'console'.

69     let console: Console;
           ~~~~~~~

  node_modules/typescript/lib/lib.dom.d.ts:19124:13
    19124 declare var console: Console;
                      ~~~~~~~
    'console' was also declared here.
  node_modules/@types/node/globals.d.ts:45:13
    45 declare var console: Console;
                   ~~~~~~~
    and here.
  node_modules/@types/node/console.d.ts:113:13
    113         var console: Console;
                    ~~~~~~~
    and here.

node_modules/@types/node/console.d.ts:113:13 - error TS2451: Cannot redeclare block-scoped variable 'console'.

113         var console: Console;
                ~~~~~~~

  node_modules/@types/k6/global.d.ts:69:9
    69     let console: Console;
               ~~~~~~~
    'console' was also declared here.

node_modules/@types/node/globals.d.ts:45:13 - error TS2451: Cannot redeclare block-scoped variable 'console'.

45 declare var console: Console;
               ~~~~~~~

  node_modules/@types/k6/global.d.ts:69:9
    69     let console: Console;
               ~~~~~~~
    'console' was also declared here.

node_modules/typescript/lib/lib.dom.d.ts:19124:13 - error TS2451: Cannot redeclare block-scoped variable 'console'.

19124 declare var console: Console;
                  ~~~~~~~

  node_modules/@types/k6/global.d.ts:69:9
    69     let console: Console;
               ~~~~~~~
    'console' was also declared here.

This is a common scenario when you want performance tests in your app pipeline.

If you switch to var you end up with this error:

node_modules/@types/k6/global.d.ts:69:9 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'console' must be of type 'Console', but here has type 'Console'.

69     var console: Console;
           ~~~~~~~

  node_modules/typescript/lib/lib.dom.d.ts:19124:13
    19124 declare var console: Console;
                      ~~~~~~~
    'console' was also declared here.

So it seems like the only option is to use --skipLibCheck which is horrible… :frowning:

I’ve created a PR to remove console from the types definitions for k6:

It’s not the ideal solution, but it’s very unlikely to impact anyone (IDEs will autocomplete ‘console’ statements without types, plus the console functionality looks like it is designed to mimic the node and browser console functionality). However the current situation blocks anyone who wants to use k6 scripts in the same repository as a TypeScript app. So it’s a pretty urgent problem for us!

It also looks like we’re not the only people to have this issue:

So it might be blocking other people from using k6 in their repos also.

Hi @larchie

PR is merged and a new TypeScript version 0.32.1 is available.

Thank you for your contribution! I agree fixing this use case is more important than having the console API available.

I wonder how you are building your k6 / node type of project. We created two templates: TypeScript and ES6 for similar cases. So, I am interested in learning more about your setup. Will send you a DM to follow up.