0

This one doesn't make sense to me:

import axios from 'axios'
import * as TE from 'fp-ts/lib/TaskEither'

export const getIntent = (sessionId: string, input: string) => process.env.INTENT_URL
  ? TE.tryCatch(
      () => axios.post(`${process.env.INTENT_URL}`,{sessionId, input}),
      reason => String(reason))
  : TE.left(Error("No INTENT_URL")
)

The Left is String and/or Error which is obviously not equal. How come this type checks???

stevemao
  • 1,423
  • 1
  • 16
  • 29

1 Answers1

3

Same reason why this typechecks:

export const getIntent = () => process.env.INTENT_URL
  ? true
  : "false"

You don't have a return type annotation on your function, so typescript automatically widens the return type to union type to cover the return values.

MnZrK
  • 1,330
  • 11
  • 23
  • Don’t you think this feels wrong? I never said I want to widen the return type. – stevemao Jan 24 '20 at 21:56
  • Dunno. I admit, it looks kinda strange, but in practice I personally never had any issues with that. Usually its a good habit to always have full type annotations on top-level functions. There is even a eslint rule to enforce it. – MnZrK Jan 24 '20 at 22:03
  • Maybe one of the strict compiler options disables such behaviour? Not sure. – MnZrK Jan 24 '20 at 22:04
  • If I want a sum type, I specify `Either` explicitly. If I want a product type, I specify `Tuple` explicitly. – stevemao Jan 24 '20 at 22:07
  • On top level you should specify the types so it's not a problem. But you might write lambda functions and don't specify the types. The example you give a simple. In practice you will have variables and may write it wrong and you don't notice it! – stevemao Jan 24 '20 at 22:10
  • As I said, in practice I have a return type annotation – MnZrK Jan 24 '20 at 22:10
  • You shouldn't use complex functions for lambdas. And for simple ones its not a problem. – MnZrK Jan 24 '20 at 22:10
  • Anyway, I am not in any relation to typescript team, so discussing whether its right or wrong behaviour is kinda pointless :) That's just how it it. – MnZrK Jan 24 '20 at 22:11
  • Not a complex one. Just one return different variables and hard to notice. – stevemao Jan 24 '20 at 22:12
  • You still gonna get an error eventually. Just a bit later in the code. – MnZrK Jan 24 '20 at 22:12
  • If its a lambda and you pass it as an argument to another higher-order function, then it will fail typechecking there - it won't fit there. – MnZrK Jan 24 '20 at 22:13