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

Your email will never ever be published.

Previous:
Find the source of an alias in bash September 29, 2025 Linux, Bash, macOS
Related by category:
A Python dict that can report which keys you did not use June 12, 2025 Python
Native connection pooling in Django 5 with PostgreSQL June 25, 2025 Python
Combining Django signals with in-memory LRU cache August 9, 2025 Python
Faster way to sum an integer series in Python August 28, 2025 Python
Related by keyword:
Run TypeScript in Node without extensions December 10, 2024 Node, JavaScript
In TypeScript, how to combine known and unknown keys to an object July 3, 2024 JavaScript
How to extend a function in TypeScript without repeating the signature types October 16, 2024 JavaScript
Node watch mode and TypeScript July 21, 2024 Node, JavaScript