Richard Robinson
Richard Robinson

Reputation: 997

TypeScript: Have return type depend on parameter

I have the following TypeScript function:

declare function getUserContext(
  options: UserContextOptions
): Promise<UserContext>;

declare function getTeamContext(
  options: TeamContextOptions
): Promise<TeamContext>;

type UserContextWithoutSlackTeamOptions = Omit<
  UserContextOptions,
  'slackTeamRef'
>;

async function getContext<
  T extends
    | TeamContextOptions
    | (TeamContextOptions & UserContextWithoutSlackTeamOptions)
>(
  options: Partial<UserContextWithoutSlackTeamOptions> & T
): Promise<
  T extends UserContextWithoutSlackTeamOptions
    ? TeamContext & UserContext
    : TeamContext
> {
  const teamContext = await getTeamContext(options);
  const userContext = options.user
    ? await getUserContext({
        user: options.user,
        slackTeamRef: teamContext.slackTeam,
      })
    : undefined;

  return {
    ...teamContext,
    ...userContext,
    octokit: userContext.octokit ?? teamContext.octokit,
  };
}

and here are the interfaces:

interface UserContextOptions {
  user: {
    id: string;
    name?: string;
  };
  slackTeamRef: string;
}

interface TeamContextOptions {
  teamId: string;
}

interface TeamContext {
  slackTeam: string;
  octokit: string;
}

interface UserContext {
  user: string | null | undefined;
  slackUser: string;
  octokit: string | undefined;
}

Logically, I would think this would work, however TypeScript complains in several places:

In the return statement, it says

TS2322: Type '{ octokit: string; user?: string | null | undefined; slackUser?: string | undefined; slackTeam: string; }' is not assignable to type 'TeamContext & UserContext'.  
 Type '{ octokit: string; user?: string | null | undefined; slackUser?: string | undefined; slackTeam: string; }' is not assignable to type 'UserContext'.    
 Property 'user' is optional in type '{ octokit: string; user?: string | null | undefined; slackUser?: string | undefined; slackTeam: string; }' but required in type 'UserContext'.

TS2532: Object is possibly 'undefined'.

I have looked at numerous other SO posts, but none of the solutions I have come across have been applicable to this specific scenario.

Any help is appreciated, thanks.

TS Playground

Upvotes: 0

Views: 91

Answers (1)

AsukaSong
AsukaSong

Reputation: 1048

Function Overloads is designed for this

async function getContext(options: TeamContextOptions & UserContextWithoutSlackTeamOptions): Promise<TeamContext & UserContext>;
async function getContext(options: TeamContextOptions): Promise<TeamContext>;
async function getContext(
  options: TeamContextOptions & UserContextWithoutSlackTeamOptions | TeamContextOptions & Partial<UserContextWithoutSlackTeamOptions>
) {
  // impl...
}

declare const teamOption: TeamContextOptions
declare const mixOption: TeamContextOptions & UserContextWithoutSlackTeamOptions

async () => {
  const v1 = await getContext(teamOption) // TeamContext
  const v2 = await getContext(mixOption) // TeamContext & UserContext
}

Upvotes: 1

Related Questions