简体   繁体   English

TypeScript:有返回类型取决于参数

[英]TypeScript: Have return type depend on parameter

I have the following TypeScript function:我有以下 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:从逻辑上讲,我认为这会起作用,但是 TypeScript 在几个地方抱怨:

In the return statement, it saysreturn声明中,它说

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.我查看了许多其他 SO 帖子,但我遇到的解决方案都没有适用于这个特定场景。

Any help is appreciated, thanks.任何帮助表示赞赏,谢谢。

TS Playground TS游乐场

Function Overloads is designed for this Function 过载是为此设计的

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM