BotBuilder-MicrosoftTeams-node icon indicating copy to clipboard operation
BotBuilder-MicrosoftTeams-node copied to clipboard

TeamsContext.team possibly undefined

Open dyladan opened this issue 5 years ago • 0 comments

Current typing is

// teamsContext.ts
export class TeamsContext {
  public get team(): models.TeamInfo {
    return this.getTeamsChannelData().team;
  }
}

// schema/models/index.ts
export interface TeamInfo {
    id?: string;
    name?: string;
}

The following code compiles correctly, but fails at runtime with a TypeError if called outside the context of a team (direct message)

// teamsCtx.team.id is undefined | string
if (teamsCtx.team.id) {
  // teamsCtx.team.id is type string
  console.log(teamsCtx.team.id)
}

TypeError: Cannot read property `id` of undefined

Suggested fix:

  1. make team an optional property of class TeamsContext: public get team(): models.TeamInfo | undefined
  2. infer return type from getTeamsChannelData.team; public get team() { (no return annotation)

dyladan avatar Jul 26 '19 18:07 dyladan