【问题标题】:TypeScript Error TS2532: Object is possibly 'undefined'?TypeScript 错误 TS2532:对象可能是“未定义”?
【发布时间】:2021-12-29 22:58:59
【问题描述】:

您好,我收到一个 TypeScript 错误对象可能未定义 这是给定一个代表相互竞争的球队的对数组,一个包含每个比赛结果的数组,编写一个返回锦标赛获胜者的函数。 Competitions 数组有 [hometeam,awayteam] 形式的元素,第二个数组结果 1 表示主队获胜,0 表示客队获胜

    if (scoreTracker!.get(teamWhoWon) > scoreTracker!.get(currentWinningTeam)) {
  currentWinningTeam = teamWhoWon
}


This is for an algorithm data challenge of this whole function::

    export function tournamentWinner(competitions: string[][], results: number[]) {
  // Write your code here.
  let currentWinningTeam = "";

  const scoreTracker: Map<string, number>= new Map();
  scoreTracker?.set(currentWinningTeam, 0)
  // since comeptitions and results have same length, use for loop to go through both of the arrays
  // they are in order of results to comeptitionns
  // use hash map to keep track of eachTeams points
  // final loop to find the team with the higest points
  // TIME COMPLEXITY O(N) = linear 1 loop.
  // if i had 2 loops O(N^2) = quadratic
  // SPACE COMPLEXITY O(K) = memory you created
  for (const index in competitions) {

    const result: number = results[index];
    // 0 0 1
    // console.log(result);

    // ["HTML", "C#"] C# > HTML
    const [homeTeam, awayTeam] = competitions[index];

    // console.log(index);
    // #C, Python, Python
    const teamWhoWon: string = result === 0 ? awayTeam : homeTeam;

    console.log('teamwhowon', teamWhoWon)

    updateScores(teamWhoWon, 3, scoreTracker)
    console.log('scoreTracker', scoreTracker)

    if (scoreTracker!.get(teamWhoWon) > scoreTracker!.get(currentWinningTeam)) {
      currentWinningTeam = teamWhoWon
    }
  }
  return currentWinningTeam;
}

function updateScores(teamWhoWon: string, points: number, scoreTracker: Map<string, number>) {
  if (!scoreTracker?.has(teamWhoWon)) {
    scoreTracker?.set(teamWhoWon, 3)
  } else {
    scoreTracker?.set(teamWhoWon, scoreTracker?.get(teamWhoWon) + points)
  }
}

console.log(
  tournamentWinner(
    [
      ['HTML', '#C'],
      ['#C', 'Python'],
      ['Python', 'HTML'],
    ],
    [0, 0, 1]
  )
);

我是 TypeScript 的新手,我不确定为什么会收到此错误。我已经定义了所有变量。

【问题讨论】:

  • TypeScript 是否显示发生错误的行?

标签: javascript typescript


【解决方案1】:

关键是您可以在处理值时添加as Type。 例如(scoreTracker.get(teamWhoWon) as number)

您可以帮助定义返回您的地图的类型。请考虑这样的修改:

//  if (scoreTracker!.get(teamWhoWon) > scoreTracker!.get(currentWinningTeam)) {
//   currentWinningTeam = teamWhoWon
// }


// This is for an algorithm data challenge of this whole function::

    export function tournamentWinner(competitions: string[][], results: number[]) {
  // Write your code here.
  let currentWinningTeam = "";

  const scoreTracker: Map<string, number>= new Map();
  scoreTracker.set(currentWinningTeam, 0)
  // since comeptitions and results have same length, use for loop to go through both of the arrays
  // they are in order of results to comeptitionns
  // use hash map to keep track of eachTeams points
  // final loop to find the team with the higest points
  // TIME COMPLEXITY O(N) = linear 1 loop.
  // if i had 2 loops O(N^2) = quadratic
  // SPACE COMPLEXITY O(K) = memory you created
  for (const index in competitions) {

    const result: number = results[index];
    // 0 0 1
    // console.log(result);

    // ["HTML", "C#"] C# > HTML
    const [homeTeam, awayTeam] = competitions[index];

    // console.log(index);
    // #C, Python, Python
    const teamWhoWon: string = result === 0 ? awayTeam : homeTeam;

    console.log('teamwhowon', teamWhoWon)

    updateScores(teamWhoWon, 3, scoreTracker)
    console.log('scoreTracker', scoreTracker)

    if ((scoreTracker.get(teamWhoWon) as number) > (scoreTracker.get(currentWinningTeam) as number)) {
      currentWinningTeam = teamWhoWon
    }
  }
  return currentWinningTeam;
}

function updateScores(teamWhoWon: string, points: number, scoreTracker: Map<string, number>) {
  if (!scoreTracker?.has(teamWhoWon)) {
    scoreTracker?.set(teamWhoWon, 3)
  } else {
    scoreTracker?.set(teamWhoWon, (scoreTracker?.get(teamWhoWon) as number) + points)
  }
}

console.log(
  tournamentWinner(
    [
      ['HTML', '#C'],
      ['#C', 'Python'],
      ['Python', 'HTML'],
    ],
    [0, 0, 1]
  )
);

【讨论】:

  • 对不起。你能详细说明“as”关键字的作用吗?为什么它可以解决我的错误?
  • 您可以使用类型断言来指定更具体的类型,因为您知道 Map 键的格式,值 typescriptlang.org/docs/handbook/2/…
猜你喜欢
  • 2022-01-05
  • 1970-01-01
  • 2021-10-07
  • 2020-12-01
  • 2022-01-20
  • 2021-11-29
  • 2021-12-15
相关资源
最近更新 更多