I am wondering if there is a way to create types that are verified by a function in Typescript/React.
Instead of a 'string' type for example, I would like it to be a regex expression:
interface Verify
{
email: /.+@.*\.com/g;
}
The regex may not work but the concept is of a prop type being a string matching the regex.
A more generalized and useful is to have the input pass through a function to verify it:
interface AcceptableInput
{
input: checkIfInputIsAcceptable(input)
}
let obj: AcceptableInput = { input: "works@working.com" }
Then to check if the input is of the correct type it would check with a function:
function checkIfInputIsAcceptable(input)
{
if(typeof input === "string)
return true;
if(input instanceof AnotherInterface)
return true;
return false;
}
The code does not work, but I hope it exemplifies the concept I am asking about. I am not sure if this is possible. Any workarounds would also be appreciated.