This might not be possible in typescript but it seems like the TS compiler should be able to figure this out some way. Example here
function validateFoo(val: str