In TypeScript, if you do this:
function canDo(maybe = false) {
console.log(maybe)
}
canDo(123)
it will complain, with
Argument of type '123' is not assignable to parameter of type 'boolean | undefined'.(2345)
That's because it infers argument maybe
to be a boolean.
In Python, with mypy
and ty
it is not so. For example:
def can_do(maybe = False):
print(maybe)
can_do(123)
Neither ty
or mypy
will complain about this. Curious.
To "fix" the problem with ty
and mypy
you have to explicitly state the type, like this:
def can_do(maybe: bool = False):
print(maybe)
can_do(123)
Now, you'll get a warning on the can_do(123)
which is
Argument 1 to "can_do" has incompatible type "int"; expected "bool" [arg-type]
I'm sure there's a good explanation for the design choice but I don't know it. For right now, I'm just trying to remember that TypeScript != Python-with-types and that if I want to be more certain of types, I can't rely on inference like I can in TypeScript.
Comments