Types of parameters 'err' and 'error' are incompatible. Type 'ExecException | null' is not assignable to type...












-1















Here is my program:



import * as child_process from 'child_process'

let global_npm_modules = ''

const initial_command = 'npm ls -g --depth=0 --json'
const shell = 'powershell'
const callback = (err: Error, stdout: string | Buffer, stderr: string | Buffer) => {
if (err) {
console.log('There was an errornn', err)
return
}

console.log(stdout)
}

child_process.exec(initial_command, {shell: shell}, callback)


And tslint is complaining about callback in the last line giving the error:



[ts]
Argument of type '(err: Error, stdout: string | Buffer, stderr: string | Buffer) => void' is not assignable to parameter of type '(error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void'.
Types of parameters 'err' and 'error' are incompatible.
Type 'ExecException | null' is not assignable to type 'Error'.
Type 'null' is not assignable to type 'Error'.
const callback: (err: Error, stdout: string | Buffer, stderr: string | Buffer) => void





I'm not quite sure what I'm missing here. I was going off the Node documentation here: https://nodejs.org/dist/latest-v11.x/docs/api/child_process.html#child_process_child_process_exec_command_options_callback

And I was just using the types provided in this section:



callback <Function> called with the output when process terminates.

error <Error>
stdout <string> | <Buffer>
stderr <string> | <Buffer>





Here is my tsconfig.json:

{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"rootDir": "src",
"outDir": "dist",
"lib": [
"esnext",
"dom"
],
"strict": true,
"esModuleInterop": true,
"sourceMap": true,
"declaration": true
},
"exclude": [
"node_modules",
"dist"
]
}


I also have @types/node installed.



EDIT



With Node, the documentation says this about child_process.exec(): "If a callback function is provided, it is called with the arguments (error, stdout, stderr). On success, error will be null."



But with TypeScript, the Error type is defined as



interface Error {
stack?: string;
}


Should I just add | null to the possible type that the error parameter can be? Or is this a bug with the @types/node package?










share|improve this question





























    -1















    Here is my program:



    import * as child_process from 'child_process'

    let global_npm_modules = ''

    const initial_command = 'npm ls -g --depth=0 --json'
    const shell = 'powershell'
    const callback = (err: Error, stdout: string | Buffer, stderr: string | Buffer) => {
    if (err) {
    console.log('There was an errornn', err)
    return
    }

    console.log(stdout)
    }

    child_process.exec(initial_command, {shell: shell}, callback)


    And tslint is complaining about callback in the last line giving the error:



    [ts]
    Argument of type '(err: Error, stdout: string | Buffer, stderr: string | Buffer) => void' is not assignable to parameter of type '(error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void'.
    Types of parameters 'err' and 'error' are incompatible.
    Type 'ExecException | null' is not assignable to type 'Error'.
    Type 'null' is not assignable to type 'Error'.
    const callback: (err: Error, stdout: string | Buffer, stderr: string | Buffer) => void





    I'm not quite sure what I'm missing here. I was going off the Node documentation here: https://nodejs.org/dist/latest-v11.x/docs/api/child_process.html#child_process_child_process_exec_command_options_callback

    And I was just using the types provided in this section:



    callback <Function> called with the output when process terminates.

    error <Error>
    stdout <string> | <Buffer>
    stderr <string> | <Buffer>





    Here is my tsconfig.json:

    {
    "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "rootDir": "src",
    "outDir": "dist",
    "lib": [
    "esnext",
    "dom"
    ],
    "strict": true,
    "esModuleInterop": true,
    "sourceMap": true,
    "declaration": true
    },
    "exclude": [
    "node_modules",
    "dist"
    ]
    }


    I also have @types/node installed.



    EDIT



    With Node, the documentation says this about child_process.exec(): "If a callback function is provided, it is called with the arguments (error, stdout, stderr). On success, error will be null."



    But with TypeScript, the Error type is defined as



    interface Error {
    stack?: string;
    }


    Should I just add | null to the possible type that the error parameter can be? Or is this a bug with the @types/node package?










    share|improve this question



























      -1












      -1








      -1








      Here is my program:



      import * as child_process from 'child_process'

      let global_npm_modules = ''

      const initial_command = 'npm ls -g --depth=0 --json'
      const shell = 'powershell'
      const callback = (err: Error, stdout: string | Buffer, stderr: string | Buffer) => {
      if (err) {
      console.log('There was an errornn', err)
      return
      }

      console.log(stdout)
      }

      child_process.exec(initial_command, {shell: shell}, callback)


      And tslint is complaining about callback in the last line giving the error:



      [ts]
      Argument of type '(err: Error, stdout: string | Buffer, stderr: string | Buffer) => void' is not assignable to parameter of type '(error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void'.
      Types of parameters 'err' and 'error' are incompatible.
      Type 'ExecException | null' is not assignable to type 'Error'.
      Type 'null' is not assignable to type 'Error'.
      const callback: (err: Error, stdout: string | Buffer, stderr: string | Buffer) => void





      I'm not quite sure what I'm missing here. I was going off the Node documentation here: https://nodejs.org/dist/latest-v11.x/docs/api/child_process.html#child_process_child_process_exec_command_options_callback

      And I was just using the types provided in this section:



      callback <Function> called with the output when process terminates.

      error <Error>
      stdout <string> | <Buffer>
      stderr <string> | <Buffer>





      Here is my tsconfig.json:

      {
      "compilerOptions": {
      "target": "es5",
      "module": "commonjs",
      "rootDir": "src",
      "outDir": "dist",
      "lib": [
      "esnext",
      "dom"
      ],
      "strict": true,
      "esModuleInterop": true,
      "sourceMap": true,
      "declaration": true
      },
      "exclude": [
      "node_modules",
      "dist"
      ]
      }


      I also have @types/node installed.



      EDIT



      With Node, the documentation says this about child_process.exec(): "If a callback function is provided, it is called with the arguments (error, stdout, stderr). On success, error will be null."



      But with TypeScript, the Error type is defined as



      interface Error {
      stack?: string;
      }


      Should I just add | null to the possible type that the error parameter can be? Or is this a bug with the @types/node package?










      share|improve this question
















      Here is my program:



      import * as child_process from 'child_process'

      let global_npm_modules = ''

      const initial_command = 'npm ls -g --depth=0 --json'
      const shell = 'powershell'
      const callback = (err: Error, stdout: string | Buffer, stderr: string | Buffer) => {
      if (err) {
      console.log('There was an errornn', err)
      return
      }

      console.log(stdout)
      }

      child_process.exec(initial_command, {shell: shell}, callback)


      And tslint is complaining about callback in the last line giving the error:



      [ts]
      Argument of type '(err: Error, stdout: string | Buffer, stderr: string | Buffer) => void' is not assignable to parameter of type '(error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void'.
      Types of parameters 'err' and 'error' are incompatible.
      Type 'ExecException | null' is not assignable to type 'Error'.
      Type 'null' is not assignable to type 'Error'.
      const callback: (err: Error, stdout: string | Buffer, stderr: string | Buffer) => void





      I'm not quite sure what I'm missing here. I was going off the Node documentation here: https://nodejs.org/dist/latest-v11.x/docs/api/child_process.html#child_process_child_process_exec_command_options_callback

      And I was just using the types provided in this section:



      callback <Function> called with the output when process terminates.

      error <Error>
      stdout <string> | <Buffer>
      stderr <string> | <Buffer>





      Here is my tsconfig.json:

      {
      "compilerOptions": {
      "target": "es5",
      "module": "commonjs",
      "rootDir": "src",
      "outDir": "dist",
      "lib": [
      "esnext",
      "dom"
      ],
      "strict": true,
      "esModuleInterop": true,
      "sourceMap": true,
      "declaration": true
      },
      "exclude": [
      "node_modules",
      "dist"
      ]
      }


      I also have @types/node installed.



      EDIT



      With Node, the documentation says this about child_process.exec(): "If a callback function is provided, it is called with the arguments (error, stdout, stderr). On success, error will be null."



      But with TypeScript, the Error type is defined as



      interface Error {
      stack?: string;
      }


      Should I just add | null to the possible type that the error parameter can be? Or is this a bug with the @types/node package?







      node.js typescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 4:04







      xiaodeaux

















      asked Nov 20 '18 at 4:27









      xiaodeauxxiaodeaux

      586




      586
























          2 Answers
          2






          active

          oldest

          votes


















          1














          The type of the first argument of callback is defined as ExecException | null and ExecException extends Error. And since a callback function is provided by the programmer to the library and would be called by the library, it should cover all the possibilities of the arguments that might be passed to it.



          Your callback: (error: Error, /* code omitted */) => ChildProcess can only handles, regarding to its type declaration, the case where error is of type Error and can not handle the case where error is null. So tsc is emitting a type error.



          I can think of two solutions to this for now:




          1. Add | null to the type declaration of error


              const callback = (err: Error | null, stdout: string | Buffer, stderr: string | Buffer) => {
          if (err) {
          console.log('There was an errornn', err)
          return
          }

          console.log(stdout)
          }



          1. Infer the expected type of the callback from existing declaration and then declare the callback with that inferred type.


              type CallbackType = typeof child_process.exec extends (cmd: string, options: any, callback: infer P) => ChildProcess ? P : any 

          const callback: CallbackType = (err, stdout, stderr) => {
          if (err) {
          console.log('There was an errornn', err)
          return
          }

          console.log(stdout)
          }


          Check it in online repl






          share|improve this answer
























          • I think I prefer method 1 for now. Thank you for looking at the problem and responding.

            – xiaodeaux
            Dec 2 '18 at 8:25



















          0














          A possible solution to this problem is to add



          "compilerOptions": {
          "strictNullChecks": false
          }


          to the tsconfig.json.



          But hopefully, in the future, I can figure out a solution that doesn't require this.



          EDIT



          Or | null can be added as a type annotation for err. I opted to choose this more specific solution.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386228%2ftypes-of-parameters-err-and-error-are-incompatible-type-execexception-nu%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The type of the first argument of callback is defined as ExecException | null and ExecException extends Error. And since a callback function is provided by the programmer to the library and would be called by the library, it should cover all the possibilities of the arguments that might be passed to it.



            Your callback: (error: Error, /* code omitted */) => ChildProcess can only handles, regarding to its type declaration, the case where error is of type Error and can not handle the case where error is null. So tsc is emitting a type error.



            I can think of two solutions to this for now:




            1. Add | null to the type declaration of error


                const callback = (err: Error | null, stdout: string | Buffer, stderr: string | Buffer) => {
            if (err) {
            console.log('There was an errornn', err)
            return
            }

            console.log(stdout)
            }



            1. Infer the expected type of the callback from existing declaration and then declare the callback with that inferred type.


                type CallbackType = typeof child_process.exec extends (cmd: string, options: any, callback: infer P) => ChildProcess ? P : any 

            const callback: CallbackType = (err, stdout, stderr) => {
            if (err) {
            console.log('There was an errornn', err)
            return
            }

            console.log(stdout)
            }


            Check it in online repl






            share|improve this answer
























            • I think I prefer method 1 for now. Thank you for looking at the problem and responding.

              – xiaodeaux
              Dec 2 '18 at 8:25
















            1














            The type of the first argument of callback is defined as ExecException | null and ExecException extends Error. And since a callback function is provided by the programmer to the library and would be called by the library, it should cover all the possibilities of the arguments that might be passed to it.



            Your callback: (error: Error, /* code omitted */) => ChildProcess can only handles, regarding to its type declaration, the case where error is of type Error and can not handle the case where error is null. So tsc is emitting a type error.



            I can think of two solutions to this for now:




            1. Add | null to the type declaration of error


                const callback = (err: Error | null, stdout: string | Buffer, stderr: string | Buffer) => {
            if (err) {
            console.log('There was an errornn', err)
            return
            }

            console.log(stdout)
            }



            1. Infer the expected type of the callback from existing declaration and then declare the callback with that inferred type.


                type CallbackType = typeof child_process.exec extends (cmd: string, options: any, callback: infer P) => ChildProcess ? P : any 

            const callback: CallbackType = (err, stdout, stderr) => {
            if (err) {
            console.log('There was an errornn', err)
            return
            }

            console.log(stdout)
            }


            Check it in online repl






            share|improve this answer
























            • I think I prefer method 1 for now. Thank you for looking at the problem and responding.

              – xiaodeaux
              Dec 2 '18 at 8:25














            1












            1








            1







            The type of the first argument of callback is defined as ExecException | null and ExecException extends Error. And since a callback function is provided by the programmer to the library and would be called by the library, it should cover all the possibilities of the arguments that might be passed to it.



            Your callback: (error: Error, /* code omitted */) => ChildProcess can only handles, regarding to its type declaration, the case where error is of type Error and can not handle the case where error is null. So tsc is emitting a type error.



            I can think of two solutions to this for now:




            1. Add | null to the type declaration of error


                const callback = (err: Error | null, stdout: string | Buffer, stderr: string | Buffer) => {
            if (err) {
            console.log('There was an errornn', err)
            return
            }

            console.log(stdout)
            }



            1. Infer the expected type of the callback from existing declaration and then declare the callback with that inferred type.


                type CallbackType = typeof child_process.exec extends (cmd: string, options: any, callback: infer P) => ChildProcess ? P : any 

            const callback: CallbackType = (err, stdout, stderr) => {
            if (err) {
            console.log('There was an errornn', err)
            return
            }

            console.log(stdout)
            }


            Check it in online repl






            share|improve this answer













            The type of the first argument of callback is defined as ExecException | null and ExecException extends Error. And since a callback function is provided by the programmer to the library and would be called by the library, it should cover all the possibilities of the arguments that might be passed to it.



            Your callback: (error: Error, /* code omitted */) => ChildProcess can only handles, regarding to its type declaration, the case where error is of type Error and can not handle the case where error is null. So tsc is emitting a type error.



            I can think of two solutions to this for now:




            1. Add | null to the type declaration of error


                const callback = (err: Error | null, stdout: string | Buffer, stderr: string | Buffer) => {
            if (err) {
            console.log('There was an errornn', err)
            return
            }

            console.log(stdout)
            }



            1. Infer the expected type of the callback from existing declaration and then declare the callback with that inferred type.


                type CallbackType = typeof child_process.exec extends (cmd: string, options: any, callback: infer P) => ChildProcess ? P : any 

            const callback: CallbackType = (err, stdout, stderr) => {
            if (err) {
            console.log('There was an errornn', err)
            return
            }

            console.log(stdout)
            }


            Check it in online repl







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 30 '18 at 2:40









            Nandiin BaoNandiin Bao

            1,051620




            1,051620













            • I think I prefer method 1 for now. Thank you for looking at the problem and responding.

              – xiaodeaux
              Dec 2 '18 at 8:25



















            • I think I prefer method 1 for now. Thank you for looking at the problem and responding.

              – xiaodeaux
              Dec 2 '18 at 8:25

















            I think I prefer method 1 for now. Thank you for looking at the problem and responding.

            – xiaodeaux
            Dec 2 '18 at 8:25





            I think I prefer method 1 for now. Thank you for looking at the problem and responding.

            – xiaodeaux
            Dec 2 '18 at 8:25













            0














            A possible solution to this problem is to add



            "compilerOptions": {
            "strictNullChecks": false
            }


            to the tsconfig.json.



            But hopefully, in the future, I can figure out a solution that doesn't require this.



            EDIT



            Or | null can be added as a type annotation for err. I opted to choose this more specific solution.






            share|improve this answer






























              0














              A possible solution to this problem is to add



              "compilerOptions": {
              "strictNullChecks": false
              }


              to the tsconfig.json.



              But hopefully, in the future, I can figure out a solution that doesn't require this.



              EDIT



              Or | null can be added as a type annotation for err. I opted to choose this more specific solution.






              share|improve this answer




























                0












                0








                0







                A possible solution to this problem is to add



                "compilerOptions": {
                "strictNullChecks": false
                }


                to the tsconfig.json.



                But hopefully, in the future, I can figure out a solution that doesn't require this.



                EDIT



                Or | null can be added as a type annotation for err. I opted to choose this more specific solution.






                share|improve this answer















                A possible solution to this problem is to add



                "compilerOptions": {
                "strictNullChecks": false
                }


                to the tsconfig.json.



                But hopefully, in the future, I can figure out a solution that doesn't require this.



                EDIT



                Or | null can be added as a type annotation for err. I opted to choose this more specific solution.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 30 '18 at 1:49

























                answered Nov 28 '18 at 0:59









                xiaodeauxxiaodeaux

                586




                586






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386228%2ftypes-of-parameters-err-and-error-are-incompatible-type-execexception-nu%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini