how do you specify a class function to use typescript props interface












0















I've just found out I can do:



interface Product {
name: string,
price: number,
category: string,
id: string,
quantity: number
}

bagTotal = (products: Product) => {

}


which is useful but I have this. where bag is one of my props coming from my redux store



interface IBagProps {
bag: {
products: Product
}
}


so in the parameters of the function I want to use the bag props from the IBagProps interface



how do I this?



I wanted to do something like this: bagTotal = (products: IBagProps) => {



but that looks like it's going to pass all the props through










share|improve this question

























  • bagTotal = (products: Product) ...?

    – quirimmo
    Dec 29 '18 at 18:55













  • @quirimmo what's the question sorry?

    – Red Baron
    Dec 29 '18 at 19:06











  • Why the function param cannot be a simple array of Product?

    – quirimmo
    Dec 29 '18 at 19:19











  • because products is an array in the bag object and bag comes from props. so I thought that it should have to live in the props interface?

    – Red Baron
    Dec 29 '18 at 19:31











  • @quirimmo does that make sense?

    – Red Baron
    Dec 29 '18 at 19:47
















0















I've just found out I can do:



interface Product {
name: string,
price: number,
category: string,
id: string,
quantity: number
}

bagTotal = (products: Product) => {

}


which is useful but I have this. where bag is one of my props coming from my redux store



interface IBagProps {
bag: {
products: Product
}
}


so in the parameters of the function I want to use the bag props from the IBagProps interface



how do I this?



I wanted to do something like this: bagTotal = (products: IBagProps) => {



but that looks like it's going to pass all the props through










share|improve this question

























  • bagTotal = (products: Product) ...?

    – quirimmo
    Dec 29 '18 at 18:55













  • @quirimmo what's the question sorry?

    – Red Baron
    Dec 29 '18 at 19:06











  • Why the function param cannot be a simple array of Product?

    – quirimmo
    Dec 29 '18 at 19:19











  • because products is an array in the bag object and bag comes from props. so I thought that it should have to live in the props interface?

    – Red Baron
    Dec 29 '18 at 19:31











  • @quirimmo does that make sense?

    – Red Baron
    Dec 29 '18 at 19:47














0












0








0








I've just found out I can do:



interface Product {
name: string,
price: number,
category: string,
id: string,
quantity: number
}

bagTotal = (products: Product) => {

}


which is useful but I have this. where bag is one of my props coming from my redux store



interface IBagProps {
bag: {
products: Product
}
}


so in the parameters of the function I want to use the bag props from the IBagProps interface



how do I this?



I wanted to do something like this: bagTotal = (products: IBagProps) => {



but that looks like it's going to pass all the props through










share|improve this question
















I've just found out I can do:



interface Product {
name: string,
price: number,
category: string,
id: string,
quantity: number
}

bagTotal = (products: Product) => {

}


which is useful but I have this. where bag is one of my props coming from my redux store



interface IBagProps {
bag: {
products: Product
}
}


so in the parameters of the function I want to use the bag props from the IBagProps interface



how do I this?



I wanted to do something like this: bagTotal = (products: IBagProps) => {



but that looks like it's going to pass all the props through







javascript reactjs typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 18:55









jonrsharpe

77.3k11103208




77.3k11103208










asked Dec 29 '18 at 18:50









Red BaronRed Baron

646




646













  • bagTotal = (products: Product) ...?

    – quirimmo
    Dec 29 '18 at 18:55













  • @quirimmo what's the question sorry?

    – Red Baron
    Dec 29 '18 at 19:06











  • Why the function param cannot be a simple array of Product?

    – quirimmo
    Dec 29 '18 at 19:19











  • because products is an array in the bag object and bag comes from props. so I thought that it should have to live in the props interface?

    – Red Baron
    Dec 29 '18 at 19:31











  • @quirimmo does that make sense?

    – Red Baron
    Dec 29 '18 at 19:47



















  • bagTotal = (products: Product) ...?

    – quirimmo
    Dec 29 '18 at 18:55













  • @quirimmo what's the question sorry?

    – Red Baron
    Dec 29 '18 at 19:06











  • Why the function param cannot be a simple array of Product?

    – quirimmo
    Dec 29 '18 at 19:19











  • because products is an array in the bag object and bag comes from props. so I thought that it should have to live in the props interface?

    – Red Baron
    Dec 29 '18 at 19:31











  • @quirimmo does that make sense?

    – Red Baron
    Dec 29 '18 at 19:47

















bagTotal = (products: Product) ...?

– quirimmo
Dec 29 '18 at 18:55







bagTotal = (products: Product) ...?

– quirimmo
Dec 29 '18 at 18:55















@quirimmo what's the question sorry?

– Red Baron
Dec 29 '18 at 19:06





@quirimmo what's the question sorry?

– Red Baron
Dec 29 '18 at 19:06













Why the function param cannot be a simple array of Product?

– quirimmo
Dec 29 '18 at 19:19





Why the function param cannot be a simple array of Product?

– quirimmo
Dec 29 '18 at 19:19













because products is an array in the bag object and bag comes from props. so I thought that it should have to live in the props interface?

– Red Baron
Dec 29 '18 at 19:31





because products is an array in the bag object and bag comes from props. so I thought that it should have to live in the props interface?

– Red Baron
Dec 29 '18 at 19:31













@quirimmo does that make sense?

– Red Baron
Dec 29 '18 at 19:47





@quirimmo does that make sense?

– Red Baron
Dec 29 '18 at 19:47












3 Answers
3






active

oldest

votes


















1














You can access to the type of the bag member simply like this:



type BagsInfo = IBagProps['bag']


You can then refactor the bagTotal function to something similar to:



function bagTotal(bagsInfo: BagsInfo) {
return bagsInfo.products.length
}


If your bagTotal function is directly "connected" to the products array you can retrieve the Product type with:



type Product = IBagProps['bags']['products'][number]





share|improve this answer
























  • doesnt seem the right way to do it?>]

    – Red Baron
    Dec 29 '18 at 19:38











  • ok, I missed the point. I need more context info: how are you connecting bagTotal to IBagProps? Is it a redux selector? as a solution you can adapt the input argument: const adapt = (fn: (products: Product) => number) => (bagProps: IBagProps) => fn(bagProps.bag.products) and then when using bagTotal just wrap with adapt(bagTotal)

    – Walter Barbagallo
    Dec 29 '18 at 19:55













  • ok I'll try and explain better. I know I can do this: bagTotal = (products: Product). but I just wanted to get it from IBagProps because I thought it makes more sense to put it in the props seeing as it's from there?

    – Red Baron
    Dec 29 '18 at 20:05



















1














It's as simple as this:



const bagTotal = (products: IBagProps['bag']['products']) => {

}


But normally you just directly use Product.



UPD, if you probably mean that IBagProps is passed to your function and you have to pull products from there, then destructuring like this can be done:



const bagTotal = ({ bag: {products} }: IBagProps) => {
products.map(p => p.name)
}





share|improve this answer


























  • that first way doesn't look correct though and I've not seen people use it like that. I understand I can just use Product but does that mean in my PropsInterface for this class that I should leave bag empty?

    – Red Baron
    Dec 30 '18 at 9:51






  • 1





    @RedBaron There is not much wrong with the first approach, however, as I said, normally you just use Product. Regarding your second question I didn't get it, sorry. I am not too familiar with react, hence with PropsInterface as well.

    – Nurbol Alpysbayev
    Dec 30 '18 at 14:32



















1














You could leverage lookup types:



class Bag extends React.Component<IBagProps> {
bagTotal = (products: IBagProps['bag']['products']) => {
/* ... */
}
}


but it is hardly the most elegant approach. If your method depends on Product, it is perfectly fine to say:



class Bag extends React.Component<IBagProps> {
bagTotal = (products: Product) => {
/* ... */
}
}


Not only is it more readable, but it also doesn't depend on the shape of your props. If one day you decide to change their structure to, let's say:



interface IBagProps {
products: Product
}


your method signature will not be affected — as it shouldn't be.






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%2f53972430%2fhow-do-you-specify-a-class-function-to-use-typescript-props-interface%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can access to the type of the bag member simply like this:



    type BagsInfo = IBagProps['bag']


    You can then refactor the bagTotal function to something similar to:



    function bagTotal(bagsInfo: BagsInfo) {
    return bagsInfo.products.length
    }


    If your bagTotal function is directly "connected" to the products array you can retrieve the Product type with:



    type Product = IBagProps['bags']['products'][number]





    share|improve this answer
























    • doesnt seem the right way to do it?>]

      – Red Baron
      Dec 29 '18 at 19:38











    • ok, I missed the point. I need more context info: how are you connecting bagTotal to IBagProps? Is it a redux selector? as a solution you can adapt the input argument: const adapt = (fn: (products: Product) => number) => (bagProps: IBagProps) => fn(bagProps.bag.products) and then when using bagTotal just wrap with adapt(bagTotal)

      – Walter Barbagallo
      Dec 29 '18 at 19:55













    • ok I'll try and explain better. I know I can do this: bagTotal = (products: Product). but I just wanted to get it from IBagProps because I thought it makes more sense to put it in the props seeing as it's from there?

      – Red Baron
      Dec 29 '18 at 20:05
















    1














    You can access to the type of the bag member simply like this:



    type BagsInfo = IBagProps['bag']


    You can then refactor the bagTotal function to something similar to:



    function bagTotal(bagsInfo: BagsInfo) {
    return bagsInfo.products.length
    }


    If your bagTotal function is directly "connected" to the products array you can retrieve the Product type with:



    type Product = IBagProps['bags']['products'][number]





    share|improve this answer
























    • doesnt seem the right way to do it?>]

      – Red Baron
      Dec 29 '18 at 19:38











    • ok, I missed the point. I need more context info: how are you connecting bagTotal to IBagProps? Is it a redux selector? as a solution you can adapt the input argument: const adapt = (fn: (products: Product) => number) => (bagProps: IBagProps) => fn(bagProps.bag.products) and then when using bagTotal just wrap with adapt(bagTotal)

      – Walter Barbagallo
      Dec 29 '18 at 19:55













    • ok I'll try and explain better. I know I can do this: bagTotal = (products: Product). but I just wanted to get it from IBagProps because I thought it makes more sense to put it in the props seeing as it's from there?

      – Red Baron
      Dec 29 '18 at 20:05














    1












    1








    1







    You can access to the type of the bag member simply like this:



    type BagsInfo = IBagProps['bag']


    You can then refactor the bagTotal function to something similar to:



    function bagTotal(bagsInfo: BagsInfo) {
    return bagsInfo.products.length
    }


    If your bagTotal function is directly "connected" to the products array you can retrieve the Product type with:



    type Product = IBagProps['bags']['products'][number]





    share|improve this answer













    You can access to the type of the bag member simply like this:



    type BagsInfo = IBagProps['bag']


    You can then refactor the bagTotal function to something similar to:



    function bagTotal(bagsInfo: BagsInfo) {
    return bagsInfo.products.length
    }


    If your bagTotal function is directly "connected" to the products array you can retrieve the Product type with:



    type Product = IBagProps['bags']['products'][number]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 29 '18 at 19:36









    Walter BarbagalloWalter Barbagallo

    165




    165













    • doesnt seem the right way to do it?>]

      – Red Baron
      Dec 29 '18 at 19:38











    • ok, I missed the point. I need more context info: how are you connecting bagTotal to IBagProps? Is it a redux selector? as a solution you can adapt the input argument: const adapt = (fn: (products: Product) => number) => (bagProps: IBagProps) => fn(bagProps.bag.products) and then when using bagTotal just wrap with adapt(bagTotal)

      – Walter Barbagallo
      Dec 29 '18 at 19:55













    • ok I'll try and explain better. I know I can do this: bagTotal = (products: Product). but I just wanted to get it from IBagProps because I thought it makes more sense to put it in the props seeing as it's from there?

      – Red Baron
      Dec 29 '18 at 20:05



















    • doesnt seem the right way to do it?>]

      – Red Baron
      Dec 29 '18 at 19:38











    • ok, I missed the point. I need more context info: how are you connecting bagTotal to IBagProps? Is it a redux selector? as a solution you can adapt the input argument: const adapt = (fn: (products: Product) => number) => (bagProps: IBagProps) => fn(bagProps.bag.products) and then when using bagTotal just wrap with adapt(bagTotal)

      – Walter Barbagallo
      Dec 29 '18 at 19:55













    • ok I'll try and explain better. I know I can do this: bagTotal = (products: Product). but I just wanted to get it from IBagProps because I thought it makes more sense to put it in the props seeing as it's from there?

      – Red Baron
      Dec 29 '18 at 20:05

















    doesnt seem the right way to do it?>]

    – Red Baron
    Dec 29 '18 at 19:38





    doesnt seem the right way to do it?>]

    – Red Baron
    Dec 29 '18 at 19:38













    ok, I missed the point. I need more context info: how are you connecting bagTotal to IBagProps? Is it a redux selector? as a solution you can adapt the input argument: const adapt = (fn: (products: Product) => number) => (bagProps: IBagProps) => fn(bagProps.bag.products) and then when using bagTotal just wrap with adapt(bagTotal)

    – Walter Barbagallo
    Dec 29 '18 at 19:55







    ok, I missed the point. I need more context info: how are you connecting bagTotal to IBagProps? Is it a redux selector? as a solution you can adapt the input argument: const adapt = (fn: (products: Product) => number) => (bagProps: IBagProps) => fn(bagProps.bag.products) and then when using bagTotal just wrap with adapt(bagTotal)

    – Walter Barbagallo
    Dec 29 '18 at 19:55















    ok I'll try and explain better. I know I can do this: bagTotal = (products: Product). but I just wanted to get it from IBagProps because I thought it makes more sense to put it in the props seeing as it's from there?

    – Red Baron
    Dec 29 '18 at 20:05





    ok I'll try and explain better. I know I can do this: bagTotal = (products: Product). but I just wanted to get it from IBagProps because I thought it makes more sense to put it in the props seeing as it's from there?

    – Red Baron
    Dec 29 '18 at 20:05













    1














    It's as simple as this:



    const bagTotal = (products: IBagProps['bag']['products']) => {

    }


    But normally you just directly use Product.



    UPD, if you probably mean that IBagProps is passed to your function and you have to pull products from there, then destructuring like this can be done:



    const bagTotal = ({ bag: {products} }: IBagProps) => {
    products.map(p => p.name)
    }





    share|improve this answer


























    • that first way doesn't look correct though and I've not seen people use it like that. I understand I can just use Product but does that mean in my PropsInterface for this class that I should leave bag empty?

      – Red Baron
      Dec 30 '18 at 9:51






    • 1





      @RedBaron There is not much wrong with the first approach, however, as I said, normally you just use Product. Regarding your second question I didn't get it, sorry. I am not too familiar with react, hence with PropsInterface as well.

      – Nurbol Alpysbayev
      Dec 30 '18 at 14:32
















    1














    It's as simple as this:



    const bagTotal = (products: IBagProps['bag']['products']) => {

    }


    But normally you just directly use Product.



    UPD, if you probably mean that IBagProps is passed to your function and you have to pull products from there, then destructuring like this can be done:



    const bagTotal = ({ bag: {products} }: IBagProps) => {
    products.map(p => p.name)
    }





    share|improve this answer


























    • that first way doesn't look correct though and I've not seen people use it like that. I understand I can just use Product but does that mean in my PropsInterface for this class that I should leave bag empty?

      – Red Baron
      Dec 30 '18 at 9:51






    • 1





      @RedBaron There is not much wrong with the first approach, however, as I said, normally you just use Product. Regarding your second question I didn't get it, sorry. I am not too familiar with react, hence with PropsInterface as well.

      – Nurbol Alpysbayev
      Dec 30 '18 at 14:32














    1












    1








    1







    It's as simple as this:



    const bagTotal = (products: IBagProps['bag']['products']) => {

    }


    But normally you just directly use Product.



    UPD, if you probably mean that IBagProps is passed to your function and you have to pull products from there, then destructuring like this can be done:



    const bagTotal = ({ bag: {products} }: IBagProps) => {
    products.map(p => p.name)
    }





    share|improve this answer















    It's as simple as this:



    const bagTotal = (products: IBagProps['bag']['products']) => {

    }


    But normally you just directly use Product.



    UPD, if you probably mean that IBagProps is passed to your function and you have to pull products from there, then destructuring like this can be done:



    const bagTotal = ({ bag: {products} }: IBagProps) => {
    products.map(p => p.name)
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 30 '18 at 3:11

























    answered Dec 30 '18 at 3:03









    Nurbol AlpysbayevNurbol Alpysbayev

    3,9721327




    3,9721327













    • that first way doesn't look correct though and I've not seen people use it like that. I understand I can just use Product but does that mean in my PropsInterface for this class that I should leave bag empty?

      – Red Baron
      Dec 30 '18 at 9:51






    • 1





      @RedBaron There is not much wrong with the first approach, however, as I said, normally you just use Product. Regarding your second question I didn't get it, sorry. I am not too familiar with react, hence with PropsInterface as well.

      – Nurbol Alpysbayev
      Dec 30 '18 at 14:32



















    • that first way doesn't look correct though and I've not seen people use it like that. I understand I can just use Product but does that mean in my PropsInterface for this class that I should leave bag empty?

      – Red Baron
      Dec 30 '18 at 9:51






    • 1





      @RedBaron There is not much wrong with the first approach, however, as I said, normally you just use Product. Regarding your second question I didn't get it, sorry. I am not too familiar with react, hence with PropsInterface as well.

      – Nurbol Alpysbayev
      Dec 30 '18 at 14:32

















    that first way doesn't look correct though and I've not seen people use it like that. I understand I can just use Product but does that mean in my PropsInterface for this class that I should leave bag empty?

    – Red Baron
    Dec 30 '18 at 9:51





    that first way doesn't look correct though and I've not seen people use it like that. I understand I can just use Product but does that mean in my PropsInterface for this class that I should leave bag empty?

    – Red Baron
    Dec 30 '18 at 9:51




    1




    1





    @RedBaron There is not much wrong with the first approach, however, as I said, normally you just use Product. Regarding your second question I didn't get it, sorry. I am not too familiar with react, hence with PropsInterface as well.

    – Nurbol Alpysbayev
    Dec 30 '18 at 14:32





    @RedBaron There is not much wrong with the first approach, however, as I said, normally you just use Product. Regarding your second question I didn't get it, sorry. I am not too familiar with react, hence with PropsInterface as well.

    – Nurbol Alpysbayev
    Dec 30 '18 at 14:32











    1














    You could leverage lookup types:



    class Bag extends React.Component<IBagProps> {
    bagTotal = (products: IBagProps['bag']['products']) => {
    /* ... */
    }
    }


    but it is hardly the most elegant approach. If your method depends on Product, it is perfectly fine to say:



    class Bag extends React.Component<IBagProps> {
    bagTotal = (products: Product) => {
    /* ... */
    }
    }


    Not only is it more readable, but it also doesn't depend on the shape of your props. If one day you decide to change their structure to, let's say:



    interface IBagProps {
    products: Product
    }


    your method signature will not be affected — as it shouldn't be.






    share|improve this answer




























      1














      You could leverage lookup types:



      class Bag extends React.Component<IBagProps> {
      bagTotal = (products: IBagProps['bag']['products']) => {
      /* ... */
      }
      }


      but it is hardly the most elegant approach. If your method depends on Product, it is perfectly fine to say:



      class Bag extends React.Component<IBagProps> {
      bagTotal = (products: Product) => {
      /* ... */
      }
      }


      Not only is it more readable, but it also doesn't depend on the shape of your props. If one day you decide to change their structure to, let's say:



      interface IBagProps {
      products: Product
      }


      your method signature will not be affected — as it shouldn't be.






      share|improve this answer


























        1












        1








        1







        You could leverage lookup types:



        class Bag extends React.Component<IBagProps> {
        bagTotal = (products: IBagProps['bag']['products']) => {
        /* ... */
        }
        }


        but it is hardly the most elegant approach. If your method depends on Product, it is perfectly fine to say:



        class Bag extends React.Component<IBagProps> {
        bagTotal = (products: Product) => {
        /* ... */
        }
        }


        Not only is it more readable, but it also doesn't depend on the shape of your props. If one day you decide to change their structure to, let's say:



        interface IBagProps {
        products: Product
        }


        your method signature will not be affected — as it shouldn't be.






        share|improve this answer













        You could leverage lookup types:



        class Bag extends React.Component<IBagProps> {
        bagTotal = (products: IBagProps['bag']['products']) => {
        /* ... */
        }
        }


        but it is hardly the most elegant approach. If your method depends on Product, it is perfectly fine to say:



        class Bag extends React.Component<IBagProps> {
        bagTotal = (products: Product) => {
        /* ... */
        }
        }


        Not only is it more readable, but it also doesn't depend on the shape of your props. If one day you decide to change their structure to, let's say:



        interface IBagProps {
        products: Product
        }


        your method signature will not be affected — as it shouldn't be.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 30 '18 at 20:15









        Karol MajewskiKarol Majewski

        1,79119




        1,79119






























            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%2f53972430%2fhow-do-you-specify-a-class-function-to-use-typescript-props-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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas