In a type system with subtypes there is a choice that people can
specify, unlike a traditional typechecker (like ML) where there is
always just one correct type.
Even in ML, if you follow the popular habit of dropping all types
except for the interface types, you might be dealing with complicated
code where a type error would make the typechecker infer the wrong
types, ending up with a huge type error which is almost completely
useless.
In addition, the Date() example shows another case for explicit types:
what if I know that it returns a string, but others won't and I want
to be explicit about wanting a string? Adding a : string would be
good in this case.
So, I changed "best" to "often preferred", and added the last thing as
an example. (Demonstrating the two points would be more verbose, so not
done.)
There are two big reasons not to omit types:
In a type system with subtypes there is a choice that people can specify, unlike a traditional typechecker (like ML) where there is always just one correct type.
Even in ML, if you follow the popular habit of dropping all types except for the interface types, you might be dealing with complicated code where a type error would make the typechecker infer the wrong types, ending up with a huge type error which is almost completely useless.
In addition, the
Date()
example shows another case for explicit types: what if I know that it returns a string, but others won't and I want to be explicit about wanting a string? Adding a: string
would be good in this case.So, I changed "best" to "often preferred", and added the last thing as an example. (Demonstrating the two points would be more verbose, so not done.)