
"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: Neither or will complain about this. Curious. To "fix" the problem with ty and mypy you have to explicitly state the type, like this:"
"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."
TypeScript often narrows inferred types for function parameters, so passing a numeric literal like 123 to a parameter inferred as boolean produces a compile-time error: Argument of type '123' is not assignable to parameter of type 'boolean | undefined'. Python type checkers like mypy and ty do not complain in the same situation when the parameter lacks an explicit annotation. Explicitly annotating the Python function parameter as bool triggers a mypy warning on can_do(123): Argument 1 to "can_do" has incompatible type "int"; expected "bool". The two ecosystems use different inference rules and require different strategies to catch type mismatches.
Read at Peterbe
Unable to calculate read time
Collection
[
|
...
]