Get type of generic parameter -


i wrote small function better handling types.

function evaluate(variable: any, type: string): {     switch (type)     {         case 'string': return string(variable);         case 'number': return isnumber(variable) ? number(variable) : -1;         case 'boolean': {             if (typeof variable === 'boolean')                 return variable;              if (typeof variable === 'string')                 return (<string>variable).tolowercase() === 'true';              if (typeof variable === 'number')                 return variable !== 0;              return false;         }         default: return null;     } }  function isnumber(n: any): boolean {     return !isnan(parsefloat(n)) && isfinite(n); } 

i try same generics, don't know how type generic parameter. it´s possible?

typeof javascript operator. can used @ run time types javascript knows about. generics typescript concept helps check correctness of code doesn't exist in compiled output. short answer no, it's not possible.

but this:

class holder<t> {     value: t;     constructor(value: t) {         this.value = value;     }     typeof(): string {         return typeof this.value;            } } 

try out.

this works because i'm operating on value inside holder, not on holder itself.


Comments

Popular posts from this blog

Issues changing the value of an element in an array in matlab -

php - MySQLi binding parameters in a prepared statement doesn't work unless inserted after "WHERE" -

vb.net - Alternative to the T-SQL AS keyword -