I am new to typescript and was wondering if there was a way to map a string value to a custom type when calling functions with templates?
for example:
object.method<TypeMapper['CustomType']>([...])
where in this case CustomType may be a custom interface such as:
interface CustomType {
x: number,
y: number
}
and the the javascript code would resolve to
object.method<CustomType>([...])
My code:
interface CustomType {
x: number,
y: number
}
interface TypeMapper { 'CustomType': CustomType }
type Test = TypeMapper['CustomType'] // CustomType
function func<T>(x: number, y: number) {
let v: T;
}
const typeVariable = 'CustomType';
const z = func<TypeMapper[typeVariable]>(1,2);
// 'typeVariable' refers to a value, but is being used as a type here.
// Did you mean 'typeof typeVariable'?(2749)
interface TypeMapper { 'CustomType': CustomType}like this?interfaceshouldn't say "found value"