Effect CommunityEC
Effect Community2y ago
15 replies
spaethnl

Resolving TypeScript Error in Effect-TS Code

For some reason I always struggle with could be instantiated with an arbitrary type which could be unrelated to type errors.

Is there a good way to get rid of that here? I'm trying to infer E and R

import { Array, pipe, Effect } from 'effect'
import * as S from "@effect/schema/Schema"
import { Api, ServerError, ApiEndpoint, Route, ApiResponse } from "effect-http"

export type HandlerFunction<Endpoint extends ApiEndpoint.ApiEndpoint.Any> = <E, R>(
  request: Route.ToRequest<ApiEndpoint.ApiEndpoint.Request<Endpoint>>,
  ) => Effect.Effect<
    ApiResponse.ApiResponse.Body<ApiEndpoint.ApiEndpoint.Response<Endpoint>>,
    E,
    R
  >

interface User { id: string }
const users: ReadonlyArray<User> = [{ id: 'user-id' }]
const MyUserApi = pipe(
  Api.get("getUser", "/user/:id"),
  Api.setRequestPath(S.Struct({ id: S.String })),
  Api.setResponseBody(S.Struct({ user: S.Struct({ id: S.String })})),
)
export const GetUser: HandlerFunction<typeof MyUserApi> = ({ path }) => {
  return pipe(
    users,
    Array.findFirst(({ id }) => path.id === id),
    Effect.mapError(() => ServerError.notFoundError(`No user with id "${path.id}"`)),
    Effect.map(user => ({ user })),
  )
}

// typescript [2322]: Type '<E, R>({ path }: { readonly path: { readonly id: string; }; }) => Effect<{ user: User; }, ServerError, never>' is not assignable to type 'HandlerFunction<ApiEndpoint<"getUser", ApiRequest<Ignored, { readonly id: string; }, Ignored, Ignored, never>, ApiResponse<200, { readonly user: { readonly id: string; }; }, Ignored, never>, Security<...>>>'.
//   Type 'Effect<{ user: User; }, ServerError, never>' is not assignable to type 'Effect<{ readonly user: { readonly id: string; }; }, E, R>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
//     Type 'ServerError' is not assignable to type 'E'.
//       'E' could be instantiated with an arbitrary type which could be unrelated to 'ServerError'.
Was this page helpful?