Passing generic class type/constructor instead of instance as parameter











up vote
0
down vote

favorite












Let's say we have a file with this class in it:



class Foo<T> {
bar: T;
constructor(bar: T) {
this.bar = bar;
}
}

function getFooType() {
return Foo;
}


In another file, the return value of getFooType() gets passed in:



function baz(fooType: ???) {
let foo1 = new fooType<number>(1);
let foo2 = new fooType<string>("2");
}


I cannot for the life of me figure out what should go into ???.



The natural choice would be Class<Foo<T>>, but that forces you close the T by doing baz<T>(...), whereas I want it to stay open; you'll get errors like "Cannot construct non-polymorphic Foo [1] with type arguments." where you do new fooFactory<...>()



So the other obvious choice is using Class<Foo>, but then you get the error "Cannot use Foo [1] without 1 type argument." on that parameter.



In fact, it looks like it's not possible to pass a class' type around if it has a generic type specifier. Is that right? If so, that's very disappointing.










share|improve this question






















  • Can you please specify the fooType content in your code ? Is it a constructor function or a class ? Because I don't see how "the return value of getFooType() gets passed in" in your current code.
    – Ivan Gabriele
    Nov 19 at 3:43










  • It's not specified because I don't know what to put there to get it to work. As for the how: dependency injection — the return value of getFooType (i.e the class Foo) gets automagically passed into baz(...) for me by my framework. Not that I see why the how actually matters.
    – Alex
    Nov 19 at 4:16















up vote
0
down vote

favorite












Let's say we have a file with this class in it:



class Foo<T> {
bar: T;
constructor(bar: T) {
this.bar = bar;
}
}

function getFooType() {
return Foo;
}


In another file, the return value of getFooType() gets passed in:



function baz(fooType: ???) {
let foo1 = new fooType<number>(1);
let foo2 = new fooType<string>("2");
}


I cannot for the life of me figure out what should go into ???.



The natural choice would be Class<Foo<T>>, but that forces you close the T by doing baz<T>(...), whereas I want it to stay open; you'll get errors like "Cannot construct non-polymorphic Foo [1] with type arguments." where you do new fooFactory<...>()



So the other obvious choice is using Class<Foo>, but then you get the error "Cannot use Foo [1] without 1 type argument." on that parameter.



In fact, it looks like it's not possible to pass a class' type around if it has a generic type specifier. Is that right? If so, that's very disappointing.










share|improve this question






















  • Can you please specify the fooType content in your code ? Is it a constructor function or a class ? Because I don't see how "the return value of getFooType() gets passed in" in your current code.
    – Ivan Gabriele
    Nov 19 at 3:43










  • It's not specified because I don't know what to put there to get it to work. As for the how: dependency injection — the return value of getFooType (i.e the class Foo) gets automagically passed into baz(...) for me by my framework. Not that I see why the how actually matters.
    – Alex
    Nov 19 at 4:16













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Let's say we have a file with this class in it:



class Foo<T> {
bar: T;
constructor(bar: T) {
this.bar = bar;
}
}

function getFooType() {
return Foo;
}


In another file, the return value of getFooType() gets passed in:



function baz(fooType: ???) {
let foo1 = new fooType<number>(1);
let foo2 = new fooType<string>("2");
}


I cannot for the life of me figure out what should go into ???.



The natural choice would be Class<Foo<T>>, but that forces you close the T by doing baz<T>(...), whereas I want it to stay open; you'll get errors like "Cannot construct non-polymorphic Foo [1] with type arguments." where you do new fooFactory<...>()



So the other obvious choice is using Class<Foo>, but then you get the error "Cannot use Foo [1] without 1 type argument." on that parameter.



In fact, it looks like it's not possible to pass a class' type around if it has a generic type specifier. Is that right? If so, that's very disappointing.










share|improve this question













Let's say we have a file with this class in it:



class Foo<T> {
bar: T;
constructor(bar: T) {
this.bar = bar;
}
}

function getFooType() {
return Foo;
}


In another file, the return value of getFooType() gets passed in:



function baz(fooType: ???) {
let foo1 = new fooType<number>(1);
let foo2 = new fooType<string>("2");
}


I cannot for the life of me figure out what should go into ???.



The natural choice would be Class<Foo<T>>, but that forces you close the T by doing baz<T>(...), whereas I want it to stay open; you'll get errors like "Cannot construct non-polymorphic Foo [1] with type arguments." where you do new fooFactory<...>()



So the other obvious choice is using Class<Foo>, but then you get the error "Cannot use Foo [1] without 1 type argument." on that parameter.



In fact, it looks like it's not possible to pass a class' type around if it has a generic type specifier. Is that right? If so, that's very disappointing.







flowtype






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 7:58









Alex

1,83232555




1,83232555












  • Can you please specify the fooType content in your code ? Is it a constructor function or a class ? Because I don't see how "the return value of getFooType() gets passed in" in your current code.
    – Ivan Gabriele
    Nov 19 at 3:43










  • It's not specified because I don't know what to put there to get it to work. As for the how: dependency injection — the return value of getFooType (i.e the class Foo) gets automagically passed into baz(...) for me by my framework. Not that I see why the how actually matters.
    – Alex
    Nov 19 at 4:16


















  • Can you please specify the fooType content in your code ? Is it a constructor function or a class ? Because I don't see how "the return value of getFooType() gets passed in" in your current code.
    – Ivan Gabriele
    Nov 19 at 3:43










  • It's not specified because I don't know what to put there to get it to work. As for the how: dependency injection — the return value of getFooType (i.e the class Foo) gets automagically passed into baz(...) for me by my framework. Not that I see why the how actually matters.
    – Alex
    Nov 19 at 4:16
















Can you please specify the fooType content in your code ? Is it a constructor function or a class ? Because I don't see how "the return value of getFooType() gets passed in" in your current code.
– Ivan Gabriele
Nov 19 at 3:43




Can you please specify the fooType content in your code ? Is it a constructor function or a class ? Because I don't see how "the return value of getFooType() gets passed in" in your current code.
– Ivan Gabriele
Nov 19 at 3:43












It's not specified because I don't know what to put there to get it to work. As for the how: dependency injection — the return value of getFooType (i.e the class Foo) gets automagically passed into baz(...) for me by my framework. Not that I see why the how actually matters.
– Alex
Nov 19 at 4:16




It's not specified because I don't know what to put there to get it to work. As for the how: dependency injection — the return value of getFooType (i.e the class Foo) gets automagically passed into baz(...) for me by my framework. Not that I see why the how actually matters.
– Alex
Nov 19 at 4:16












1 Answer
1






active

oldest

votes

















up vote
0
down vote













So you just have to describe the typings the same way you call your factory function:



function baz(fooType: <T>(bar: T) => Foo<T>) {
let foo1 = new fooType<number>(1);
let foo2 = new fooType<string>("2");
}


Note:



Knowing that it uses a dependency injection mechanism was in fact quite important since that solves the issue of the generic declaration. However <T> means that typeof T equals any. You should specify something more accurate like <T: number | string>.






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',
    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%2f53246846%2fpassing-generic-class-type-constructor-instead-of-instance-as-parameter%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








    up vote
    0
    down vote













    So you just have to describe the typings the same way you call your factory function:



    function baz(fooType: <T>(bar: T) => Foo<T>) {
    let foo1 = new fooType<number>(1);
    let foo2 = new fooType<string>("2");
    }


    Note:



    Knowing that it uses a dependency injection mechanism was in fact quite important since that solves the issue of the generic declaration. However <T> means that typeof T equals any. You should specify something more accurate like <T: number | string>.






    share|improve this answer



























      up vote
      0
      down vote













      So you just have to describe the typings the same way you call your factory function:



      function baz(fooType: <T>(bar: T) => Foo<T>) {
      let foo1 = new fooType<number>(1);
      let foo2 = new fooType<string>("2");
      }


      Note:



      Knowing that it uses a dependency injection mechanism was in fact quite important since that solves the issue of the generic declaration. However <T> means that typeof T equals any. You should specify something more accurate like <T: number | string>.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        So you just have to describe the typings the same way you call your factory function:



        function baz(fooType: <T>(bar: T) => Foo<T>) {
        let foo1 = new fooType<number>(1);
        let foo2 = new fooType<string>("2");
        }


        Note:



        Knowing that it uses a dependency injection mechanism was in fact quite important since that solves the issue of the generic declaration. However <T> means that typeof T equals any. You should specify something more accurate like <T: number | string>.






        share|improve this answer














        So you just have to describe the typings the same way you call your factory function:



        function baz(fooType: <T>(bar: T) => Foo<T>) {
        let foo1 = new fooType<number>(1);
        let foo2 = new fooType<string>("2");
        }


        Note:



        Knowing that it uses a dependency injection mechanism was in fact quite important since that solves the issue of the generic declaration. However <T> means that typeof T equals any. You should specify something more accurate like <T: number | string>.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 4:43

























        answered Nov 19 at 4:38









        Ivan Gabriele

        3,26622444




        3,26622444






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53246846%2fpassing-generic-class-type-constructor-instead-of-instance-as-parameter%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

            Full-time equivalent

            Bicuculline

            さくらももこ