I would like to check whether provided value match defined regex-match string type. Based on that I would return proper value. I need that to properly override my method.
EXAMPLE
type RegexType =`${string}/:${string}`
function myFunction(url: string): string;
function myFunction<P extends RegexType>(
url: P,
): number;
function myFunction<P extends string>(value: P) {
// value is typeof RegexType if yes then reutrn numberFunction otherwise stringFunction
}
const numberFunction = (): number => 1;
const stringFunction = (): string => '1';
I know that I can use regex inside of the function, but then I meet another problem, that type is not properly interfered for template string
type RegexType =`${string}/:${string}`
function myFunction<P extends RegexType>(
url: P,
): number;
function myFunction(url: string): string;
function myFunction<P extends string>(value: P) {
// value is typeof RegexType if yes then reutrn numberFunction otherwise stringFunction
return value.match('\/:') ? numberFunction() : stringFunction();
}
const numberFunction = (): number => 1;
const stringFunction = (): string => '1';
const testValue = '123/:123';
// ERROR
const valueNum: number = myFunction(`${testValue}`);
// VALID
const valueNum123: number = myFunction('123/:123');