How would you add type hints to this function?
JavaScript
x
3
1
def cast_to(x, ty=bool):
2
return ty(x)
3
Advertisement
Answer
Hmm it seems you can do it like this:
JavaScript
1
7
1
from typing import Type, TypeVar, Any
2
3
T = TypeVar('T')
4
5
def cast_to(x: Any, ty: Type[T] = bool) -> T:
6
return ty(x)
7