I try to cast a function inside an object to a type.
Why does this not work?
export type Validator<TInput> = (
input: TInput
) => Promise<{ [key: string]: string }>;
const works: Validator<string> = async (input) => {
return {};
};
const doesNotWork = {
myFancyProperty: Validator<string> = async (input) => {
return {};
}
}
=> 'Validator' only refers to a type, but is being used as a value here.
What’s the correct syntax? I’ve searched and tried for an hour now. Maybe i am to tired.
You can’t specify a property type in that way.
Your best bet is to type the object, rather than the property:
But in rare cases, you might use a type assertion on the value you’re assigning: