Why does typescript give type `never` when excluding extended interface












1















I created a function to strip an object of an interface. However Typescript (Version 3.2.2) now claims the type is never when it should be a type with property child



interface Child extends Parent {
child: string
}

interface Parent {
parent: string
}

const a: Child = { child: "", parent: "" }

const b = removeParent(a);

function removeParent<T extends Parent>(obj: T) {
delete obj.parent;
return obj as Exclude<T, Parent>;
}

// b is now type never...


This does work:



function removeParent<T extends Parent>(obj: T) {
delete obj.parent;
type Without<T, K> = Pick<T, Exclude<keyof T, K>>;
return obj as Without<T, "parent">;
}


However I want a generic solution that doesn't require me writing out the types to exclude.










share|improve this question





























    1















    I created a function to strip an object of an interface. However Typescript (Version 3.2.2) now claims the type is never when it should be a type with property child



    interface Child extends Parent {
    child: string
    }

    interface Parent {
    parent: string
    }

    const a: Child = { child: "", parent: "" }

    const b = removeParent(a);

    function removeParent<T extends Parent>(obj: T) {
    delete obj.parent;
    return obj as Exclude<T, Parent>;
    }

    // b is now type never...


    This does work:



    function removeParent<T extends Parent>(obj: T) {
    delete obj.parent;
    type Without<T, K> = Pick<T, Exclude<keyof T, K>>;
    return obj as Without<T, "parent">;
    }


    However I want a generic solution that doesn't require me writing out the types to exclude.










    share|improve this question



























      1












      1








      1








      I created a function to strip an object of an interface. However Typescript (Version 3.2.2) now claims the type is never when it should be a type with property child



      interface Child extends Parent {
      child: string
      }

      interface Parent {
      parent: string
      }

      const a: Child = { child: "", parent: "" }

      const b = removeParent(a);

      function removeParent<T extends Parent>(obj: T) {
      delete obj.parent;
      return obj as Exclude<T, Parent>;
      }

      // b is now type never...


      This does work:



      function removeParent<T extends Parent>(obj: T) {
      delete obj.parent;
      type Without<T, K> = Pick<T, Exclude<keyof T, K>>;
      return obj as Without<T, "parent">;
      }


      However I want a generic solution that doesn't require me writing out the types to exclude.










      share|improve this question
















      I created a function to strip an object of an interface. However Typescript (Version 3.2.2) now claims the type is never when it should be a type with property child



      interface Child extends Parent {
      child: string
      }

      interface Parent {
      parent: string
      }

      const a: Child = { child: "", parent: "" }

      const b = removeParent(a);

      function removeParent<T extends Parent>(obj: T) {
      delete obj.parent;
      return obj as Exclude<T, Parent>;
      }

      // b is now type never...


      This does work:



      function removeParent<T extends Parent>(obj: T) {
      delete obj.parent;
      type Without<T, K> = Pick<T, Exclude<keyof T, K>>;
      return obj as Without<T, "parent">;
      }


      However I want a generic solution that doesn't require me writing out the types to exclude.







      typescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 16:36







      Red Riding Hood

















      asked Jan 2 at 16:05









      Red Riding HoodRed Riding Hood

      552415




      552415
























          1 Answer
          1






          active

          oldest

          votes


















          3














          I think you do not want Exclude<T, Parent>



          Exclude is essentially:
          type Exclude<T, U> = T extends U ? never : T



          and as a result, your return type will return never since T extends Parent in your usage.



          What I think you're really after is:
          Pick<T, Exclude<keyof T, keyof Parent>>



          so that you're saying 'pick all properties of T that do not exist int he parent'



          function removeOneAndTwo<T extends Parent>(obj: T): Pick<T, Exclude<keyof T, keyof Parent>> {
          delete obj.one;
          delete obj.two;
          return obj;
          }





          share|improve this answer



















          • 1





            Exclude is really intended for removing branches from a union type. For example Exclude<string|null|void, null|void>string, or for excluding elements from a keyof type like in this answer.

            – Jesse Hallett
            Jan 2 at 16:52













          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%2f54009493%2fwhy-does-typescript-give-type-never-when-excluding-extended-interface%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          I think you do not want Exclude<T, Parent>



          Exclude is essentially:
          type Exclude<T, U> = T extends U ? never : T



          and as a result, your return type will return never since T extends Parent in your usage.



          What I think you're really after is:
          Pick<T, Exclude<keyof T, keyof Parent>>



          so that you're saying 'pick all properties of T that do not exist int he parent'



          function removeOneAndTwo<T extends Parent>(obj: T): Pick<T, Exclude<keyof T, keyof Parent>> {
          delete obj.one;
          delete obj.two;
          return obj;
          }





          share|improve this answer



















          • 1





            Exclude is really intended for removing branches from a union type. For example Exclude<string|null|void, null|void>string, or for excluding elements from a keyof type like in this answer.

            – Jesse Hallett
            Jan 2 at 16:52


















          3














          I think you do not want Exclude<T, Parent>



          Exclude is essentially:
          type Exclude<T, U> = T extends U ? never : T



          and as a result, your return type will return never since T extends Parent in your usage.



          What I think you're really after is:
          Pick<T, Exclude<keyof T, keyof Parent>>



          so that you're saying 'pick all properties of T that do not exist int he parent'



          function removeOneAndTwo<T extends Parent>(obj: T): Pick<T, Exclude<keyof T, keyof Parent>> {
          delete obj.one;
          delete obj.two;
          return obj;
          }





          share|improve this answer



















          • 1





            Exclude is really intended for removing branches from a union type. For example Exclude<string|null|void, null|void>string, or for excluding elements from a keyof type like in this answer.

            – Jesse Hallett
            Jan 2 at 16:52
















          3












          3








          3







          I think you do not want Exclude<T, Parent>



          Exclude is essentially:
          type Exclude<T, U> = T extends U ? never : T



          and as a result, your return type will return never since T extends Parent in your usage.



          What I think you're really after is:
          Pick<T, Exclude<keyof T, keyof Parent>>



          so that you're saying 'pick all properties of T that do not exist int he parent'



          function removeOneAndTwo<T extends Parent>(obj: T): Pick<T, Exclude<keyof T, keyof Parent>> {
          delete obj.one;
          delete obj.two;
          return obj;
          }





          share|improve this answer













          I think you do not want Exclude<T, Parent>



          Exclude is essentially:
          type Exclude<T, U> = T extends U ? never : T



          and as a result, your return type will return never since T extends Parent in your usage.



          What I think you're really after is:
          Pick<T, Exclude<keyof T, keyof Parent>>



          so that you're saying 'pick all properties of T that do not exist int he parent'



          function removeOneAndTwo<T extends Parent>(obj: T): Pick<T, Exclude<keyof T, keyof Parent>> {
          delete obj.one;
          delete obj.two;
          return obj;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 16:43









          EstebanEsteban

          1,98011014




          1,98011014








          • 1





            Exclude is really intended for removing branches from a union type. For example Exclude<string|null|void, null|void>string, or for excluding elements from a keyof type like in this answer.

            – Jesse Hallett
            Jan 2 at 16:52
















          • 1





            Exclude is really intended for removing branches from a union type. For example Exclude<string|null|void, null|void>string, or for excluding elements from a keyof type like in this answer.

            – Jesse Hallett
            Jan 2 at 16:52










          1




          1





          Exclude is really intended for removing branches from a union type. For example Exclude<string|null|void, null|void>string, or for excluding elements from a keyof type like in this answer.

          – Jesse Hallett
          Jan 2 at 16:52







          Exclude is really intended for removing branches from a union type. For example Exclude<string|null|void, null|void>string, or for excluding elements from a keyof type like in this answer.

          – Jesse Hallett
          Jan 2 at 16:52






















          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%2f54009493%2fwhy-does-typescript-give-type-never-when-excluding-extended-interface%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







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory