Search code examples
typescripttypescript-typingstypecheckingfp-ts

Why does it type check?


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???


Solution

  • 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.