Passing generic class type/constructor instead of instance as parameter
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
add a comment |
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
Can you please specify thefooType
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 ofgetFooType
(i.e the classFoo
) gets automagically passed intobaz(...)
for me by my framework. Not that I see why the how actually matters.
– Alex
Nov 19 at 4:16
add a comment |
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
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
flowtype
asked Nov 11 at 7:58
Alex
1,84732555
1,84732555
Can you please specify thefooType
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 ofgetFooType
(i.e the classFoo
) gets automagically passed intobaz(...)
for me by my framework. Not that I see why the how actually matters.
– Alex
Nov 19 at 4:16
add a comment |
Can you please specify thefooType
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 ofgetFooType
(i.e the classFoo
) gets automagically passed intobaz(...)
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
add a comment |
1 Answer
1
active
oldest
votes
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>
.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
.
add a comment |
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>
.
add a comment |
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>
.
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>
.
edited Nov 19 at 4:43
answered Nov 19 at 4:38
Ivan Gabriele
3,29622444
3,29622444
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 classFoo
) gets automagically passed intobaz(...)
for me by my framework. Not that I see why the how actually matters.– Alex
Nov 19 at 4:16