3 ms·
Type annotation is not shit. It’s a contract. What’s the point of you discovering the return type is a string. If I write a function and I set the return type
by WirelessGigabit 4y ago
Type annotation is not shit. It’s a contract.
What’s the point of you discovering the return type is a string. If I write a function and I set the return type to string and return a number it blows up on my face. With a deduction that’s not the case.
- danielEM 4y agoYou're missing a key point here, this is JavaScript, so there is no type annotations. And even if there were and inference was available as well then in most of cases I don't think you would need to annotate a type of function, as at some point it would 'blow up your face' anyway when mismatch of types.
- WirelessGigabit 4y ago// bar.foo is number // getFoo() returns a number bar.foo += getFoo(); // getFoo() has a bug and now returns a string. Nothing fails. If getFoo() had a return type of number this wouldn’t be the case.
- IshKebab 4y agoJavaScript has type annotations, they're called Typescript. And this doesn't seem to be sold as an automated way to convert JS code to TS (which would be really useful). It seems like they intend for you to stick to pure JS code which makes no sense really. > at some point it would 'blow up your face' anyway I'm not sure exactly what you're saying but yes you do need to annotate the types of functions. Global type inference is possible but it has performance issues, makes type errors way harder to debug and obscures the intent of code. Types are a part of your interface. They should be stated, not inferred. I would rather things didn't blow my face up.
- Spivak 4y agoOdd, I feel wildly different about the “point” of types. For me, in cases where the type checker correctly infers the type saves me work. If I depend on the return type of my function being string and I change it the type checker will complain at the call site making the annotation pointless. If I don’t depend on it anywhere I can safely change it again making the annotation pointless. For me the type checker exists to help me understand relationships and integration points, any time spent fucking with the types for its own sake is a waste. If I find a situation where updating the code would require I update the types I’ve just written the same thing twice for some reason. I want the type checker to tell me when things are genuinely bugs in my code because of incompatible types not because it doesn’t match the annotation. Even in libraries where I can’t see all the callsites all my promises to the caller are encoded in the tests which to me is a stronger guarantee and the type checker will still catch it just over there.