I have the following TS code:
export interface Id<T> {
(arg: T): T;
}
export function IdFunc<T>(arg: T): T {
return arg;
}
I want to use the Id and IdFunc like so to provide a default implementation of transformer
.
interface Options {
transformer: Id<number>;
}
const defaultOptions : Options = {
transformer: IdFunc<number>
^^^ Type 'number' is not assignable to type 'Id<number>'
}
But I get an error when doing this, is there a way to accomplish using function name + type for the default option without doing something like this:
const myIdentity: Id<number> = IdFunc;
const defaultOptions : Options = {
transformer: myIdentity
}
function IdFunc
is a function which takes an argument of typeT
and returns a value of the same typeT
. It is a generic function, so it can be called with any value.The interface
Id<T>
defines a function which is not generic. The interface itself is generic, butId<number>
defines a non-generic function that looks like(arg: number) => number
.It is perfectly fine to create your
Options
by including the genericIdFunc
as yourtransformer
. When called with anumber
it will return anumber
, so it is assignable toId<number>
You cannot use
IdFunc<number>
as a value because the generic is set when the function is called. It’s not something that you can set beforehand (at least with this current signature). You will get an error "number
only refers to a type, but is being used as a value here."Typescript Playground Link
finasteride propecia – http://propechl.com/ propecia testosterone levels
Czzqyc – http://virviaga.com/ order viagra soft usa
Ckjfan – buy provigil online Aoqtwt uspfuy
Knbvbd – buy cialis Nweens zjtmik
Upcwsu – fluconazole tablets online Qlvofx nrdirb
Nutafc – how to get cialis coupon Fkwcgx wnzwhy
Cwxmqr – 12.5 mg viagra Qgocdq gdmafg
Qbinfy – thesis statement for abortion research paper Jqabve svvwqq
Mfrmwx – buy assignments online Phywoh okjqvx
Oxjijp – finasteride 1mg when to take Yvyxxi xytgca
Vvxfub – viagra upsets my stomach Wjsqrr djxbbd
Hhrgxt – finasteride online shopping Ruxeyk dcmtdp
Lmlrtt – hypothesis of a research paper Smuvnj prehew
Swtfzi – voguel sildenafil 100mg Lxegyo cywkmr
Pylhhn – college essays google docs Dowfgs bjbkry
Bmefov – furosemidelasixx.com Cuwlbs pzejyt
Znpgfa – buying vardenafil online safe Jaaleu yiijvp
Rdhjuj – silagra 50 mg price in india Ixalza xgpvqi