I'm new to Typescript (come from C#) and i'm struggeling with the "special" generic implementation.
Here is my code:
function createValue<TValue extends string | boolean>(): TValue[]
{
let result = new Array<TValue>();
if (typeof TValue === "string") // error 'TValue' only refers to a type, but is being used as a value here
{
result[0] = "abc" as TValue;
}
else
{
result[0] = false as TValue;
}
return result;
}
console.log(createValue<boolean>());
How did i get the type?
I'm allready tried pretty ugly solutions to create a value, but this also don't behave as expected.
let value : TValue = new Array<TValue>(1)[0];
let type = typeof value; // undefined
There exists several solutions for object types but i didn't get it for primitives.
Could you give me any help?
Thanks