typescript remove optional property












2















I'm trying to build a dynamic type for a builder



export type Builder<T, K extends keyof T> = {
[P in K]: (value: T[P]) => Builder<T, K>;
} & {
build(): Readonly<T>;
};


If I have a class or an interface with optional properties, I get this kind of error:



$ tsc
test/OtherClass.ts:29:1 - error TS2722: Cannot invoke an object which is possibly 'undefined'.

29 OtherClass.builder().patate(Patate.AU_FOUR).build();
~~~~~~~~~~~~~~~~~~~~~~~~~~~


This is my class



export class OtherClass {
constructor(
public literal: string | undefined,
public patate?: Patate,
public hello: Readonly<Hello> = {},
) { }

static builder: () => Builder<OtherClass, keyof OtherClass> = builder.for(OtherClass);
}


I was expecting the type to create a builder with a non-optional method for each properties, but for some reason, the optionality of patate seem inherent to the key and not the type. I don't get this behavior with the property literal



It looks like an issue to me. I'm using typescript 3.1.4. Is there another way to remove the question mark dynamically?



I've tried to use the NonNullable helper to create first a copy of my type with nothing nullable, but patate remains optional.



This is the effective type that vscode gives me



(property) patate?: ((value: Patate | undefined) => Builder<OtherClass, "literal" | "patate" | "hello">) | undefined









share|improve this question



























    2















    I'm trying to build a dynamic type for a builder



    export type Builder<T, K extends keyof T> = {
    [P in K]: (value: T[P]) => Builder<T, K>;
    } & {
    build(): Readonly<T>;
    };


    If I have a class or an interface with optional properties, I get this kind of error:



    $ tsc
    test/OtherClass.ts:29:1 - error TS2722: Cannot invoke an object which is possibly 'undefined'.

    29 OtherClass.builder().patate(Patate.AU_FOUR).build();
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~


    This is my class



    export class OtherClass {
    constructor(
    public literal: string | undefined,
    public patate?: Patate,
    public hello: Readonly<Hello> = {},
    ) { }

    static builder: () => Builder<OtherClass, keyof OtherClass> = builder.for(OtherClass);
    }


    I was expecting the type to create a builder with a non-optional method for each properties, but for some reason, the optionality of patate seem inherent to the key and not the type. I don't get this behavior with the property literal



    It looks like an issue to me. I'm using typescript 3.1.4. Is there another way to remove the question mark dynamically?



    I've tried to use the NonNullable helper to create first a copy of my type with nothing nullable, but patate remains optional.



    This is the effective type that vscode gives me



    (property) patate?: ((value: Patate | undefined) => Builder<OtherClass, "literal" | "patate" | "hello">) | undefined









    share|improve this question

























      2












      2








      2


      0






      I'm trying to build a dynamic type for a builder



      export type Builder<T, K extends keyof T> = {
      [P in K]: (value: T[P]) => Builder<T, K>;
      } & {
      build(): Readonly<T>;
      };


      If I have a class or an interface with optional properties, I get this kind of error:



      $ tsc
      test/OtherClass.ts:29:1 - error TS2722: Cannot invoke an object which is possibly 'undefined'.

      29 OtherClass.builder().patate(Patate.AU_FOUR).build();
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~


      This is my class



      export class OtherClass {
      constructor(
      public literal: string | undefined,
      public patate?: Patate,
      public hello: Readonly<Hello> = {},
      ) { }

      static builder: () => Builder<OtherClass, keyof OtherClass> = builder.for(OtherClass);
      }


      I was expecting the type to create a builder with a non-optional method for each properties, but for some reason, the optionality of patate seem inherent to the key and not the type. I don't get this behavior with the property literal



      It looks like an issue to me. I'm using typescript 3.1.4. Is there another way to remove the question mark dynamically?



      I've tried to use the NonNullable helper to create first a copy of my type with nothing nullable, but patate remains optional.



      This is the effective type that vscode gives me



      (property) patate?: ((value: Patate | undefined) => Builder<OtherClass, "literal" | "patate" | "hello">) | undefined









      share|improve this question














      I'm trying to build a dynamic type for a builder



      export type Builder<T, K extends keyof T> = {
      [P in K]: (value: T[P]) => Builder<T, K>;
      } & {
      build(): Readonly<T>;
      };


      If I have a class or an interface with optional properties, I get this kind of error:



      $ tsc
      test/OtherClass.ts:29:1 - error TS2722: Cannot invoke an object which is possibly 'undefined'.

      29 OtherClass.builder().patate(Patate.AU_FOUR).build();
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~


      This is my class



      export class OtherClass {
      constructor(
      public literal: string | undefined,
      public patate?: Patate,
      public hello: Readonly<Hello> = {},
      ) { }

      static builder: () => Builder<OtherClass, keyof OtherClass> = builder.for(OtherClass);
      }


      I was expecting the type to create a builder with a non-optional method for each properties, but for some reason, the optionality of patate seem inherent to the key and not the type. I don't get this behavior with the property literal



      It looks like an issue to me. I'm using typescript 3.1.4. Is there another way to remove the question mark dynamically?



      I've tried to use the NonNullable helper to create first a copy of my type with nothing nullable, but patate remains optional.



      This is the effective type that vscode gives me



      (property) patate?: ((value: Patate | undefined) => Builder<OtherClass, "literal" | "patate" | "hello">) | undefined






      typescript typescript3.0






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 3:07









      FrancisFrancis

      2,7371243




      2,7371243
























          1 Answer
          1






          active

          oldest

          votes


















          3














          Does this help?



          export type Builder<T, K extends keyof T> = {
          [P in K]-?: (value: T[P]) => Builder<T, K>;
          } & {
          build(): Readonly<T>;
          };


          Note the -? which does what you want - removes optionality.






          share|improve this answer
























          • Yes thanks, but do you know why it inherits the optionality from the key name?

            – Francis
            Nov 15 '18 at 14:00






          • 3





            When the "constraint" of a mapped type (the type after in, which determines the set of properties) is of the form keyof T or is a type parameter constrained by keyof T for some type T, the mapped type is "homomorphic" and copies modifiers (optional and readonly) from T. There's a bit about this here.

            – Matt McCutchen
            Nov 15 '18 at 14:09











          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%2f53311845%2ftypescript-remove-optional-property%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














          Does this help?



          export type Builder<T, K extends keyof T> = {
          [P in K]-?: (value: T[P]) => Builder<T, K>;
          } & {
          build(): Readonly<T>;
          };


          Note the -? which does what you want - removes optionality.






          share|improve this answer
























          • Yes thanks, but do you know why it inherits the optionality from the key name?

            – Francis
            Nov 15 '18 at 14:00






          • 3





            When the "constraint" of a mapped type (the type after in, which determines the set of properties) is of the form keyof T or is a type parameter constrained by keyof T for some type T, the mapped type is "homomorphic" and copies modifiers (optional and readonly) from T. There's a bit about this here.

            – Matt McCutchen
            Nov 15 '18 at 14:09
















          3














          Does this help?



          export type Builder<T, K extends keyof T> = {
          [P in K]-?: (value: T[P]) => Builder<T, K>;
          } & {
          build(): Readonly<T>;
          };


          Note the -? which does what you want - removes optionality.






          share|improve this answer
























          • Yes thanks, but do you know why it inherits the optionality from the key name?

            – Francis
            Nov 15 '18 at 14:00






          • 3





            When the "constraint" of a mapped type (the type after in, which determines the set of properties) is of the form keyof T or is a type parameter constrained by keyof T for some type T, the mapped type is "homomorphic" and copies modifiers (optional and readonly) from T. There's a bit about this here.

            – Matt McCutchen
            Nov 15 '18 at 14:09














          3












          3








          3







          Does this help?



          export type Builder<T, K extends keyof T> = {
          [P in K]-?: (value: T[P]) => Builder<T, K>;
          } & {
          build(): Readonly<T>;
          };


          Note the -? which does what you want - removes optionality.






          share|improve this answer













          Does this help?



          export type Builder<T, K extends keyof T> = {
          [P in K]-?: (value: T[P]) => Builder<T, K>;
          } & {
          build(): Readonly<T>;
          };


          Note the -? which does what you want - removes optionality.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 3:24









          Nurbol AlpysbayevNurbol Alpysbayev

          3,9621227




          3,9621227













          • Yes thanks, but do you know why it inherits the optionality from the key name?

            – Francis
            Nov 15 '18 at 14:00






          • 3





            When the "constraint" of a mapped type (the type after in, which determines the set of properties) is of the form keyof T or is a type parameter constrained by keyof T for some type T, the mapped type is "homomorphic" and copies modifiers (optional and readonly) from T. There's a bit about this here.

            – Matt McCutchen
            Nov 15 '18 at 14:09



















          • Yes thanks, but do you know why it inherits the optionality from the key name?

            – Francis
            Nov 15 '18 at 14:00






          • 3





            When the "constraint" of a mapped type (the type after in, which determines the set of properties) is of the form keyof T or is a type parameter constrained by keyof T for some type T, the mapped type is "homomorphic" and copies modifiers (optional and readonly) from T. There's a bit about this here.

            – Matt McCutchen
            Nov 15 '18 at 14:09

















          Yes thanks, but do you know why it inherits the optionality from the key name?

          – Francis
          Nov 15 '18 at 14:00





          Yes thanks, but do you know why it inherits the optionality from the key name?

          – Francis
          Nov 15 '18 at 14:00




          3




          3





          When the "constraint" of a mapped type (the type after in, which determines the set of properties) is of the form keyof T or is a type parameter constrained by keyof T for some type T, the mapped type is "homomorphic" and copies modifiers (optional and readonly) from T. There's a bit about this here.

          – Matt McCutchen
          Nov 15 '18 at 14:09





          When the "constraint" of a mapped type (the type after in, which determines the set of properties) is of the form keyof T or is a type parameter constrained by keyof T for some type T, the mapped type is "homomorphic" and copies modifiers (optional and readonly) from T. There's a bit about this here.

          – Matt McCutchen
          Nov 15 '18 at 14:09


















          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%2f53311845%2ftypescript-remove-optional-property%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







          這個網誌中的熱門文章

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Xamarin.form Move up view when keyboard appear

          JBPM : POST request for execute process go wrong