Effect CommunityEC
Effect Community2y ago
5 replies
ryan

Narrowing Function Result in TypeScript for Specific Routes

Ok so i have a typescript problem where i'm trying to narrow this function result.

I have a get function like so:

get: (route, init) => {
    const result = Match.type<Routes>().pipe(
      Match.when('/tokens', () =>
        Effect.succeed({ status: 200 as const, body: tokenResponseBody }),
      ),
      Match.when('/userinfo', () =>
        Effect.succeed({ status: 200 as const, body: userInfoResponse }),
      ),
      Match.when('/authorize', () => pipe(init['query'], getFirstElementAndRespond)),
      Match.exhaustive,
    )(route);
    return result;
  }


route is typed as Routes which is a union of strings that a route can be.

The call site looks like this:

    const response: GetAuthorizeSuccess = yield* get('/authorize', {


Basically, i want to narrow based on the parameter i pass in - so that in my handler it narrows the result to just one of the types instead of the union that get says it can return.
Was this page helpful?