简体   繁体   English

TypeScript 错误 TS2532:Object 可能是“未定义”?

[英]TypeScript Error TS2532: Object is possibly 'undefined'?

Hello I am getting a TypeScript error object is possibly undefined for This is for given an array of pairs representing the teams that have competed against each other an array containing the results of each competition, write a function that returns the winner of the tournament.您好,我收到 TypeScript 错误 object 可能是未定义的,这是因为给定一组代表已相互竞争的团队的对数组包含每场比赛的结果的数组,编写一个 ZC1C425268E68385D1AB5074C 锦标赛的获胜者。 Comeptitions array has the elemtns in form of [hometeam, awayteam] the second array results 1 means the home team won and 0 means the away team won比赛数组有 [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]
  )
);

I am new to TypeScript and I'm not sure why I am getting this error.我是 TypeScript 的新手,我不确定为什么会收到此错误。 I've defined all my variables.我已经定义了所有变量。

The key thing is that you could add as Type when working with values.关键是您可以在使用值时添加as Type For example (scoreTracker.get(teamWhoWon) as number) .例如(scoreTracker.get(teamWhoWon) as number)

You could help with definition of type which returns your Map.您可以帮助定义返回 Map 的类型。 Please consider such edits:请考虑这样的编辑:

 // 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] ) );

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

相关问题 打字稿数组语义错误 TS2532:对象可能是“未定义” - Typescript array semantic error TS2532: Object is possibly 'undefined' TypeScript 2:Import语句生成TS2532:“对象可能是&#39;undefined&#39;。” - TypeScript 2: Import statement generates TS2532: “Object is possibly 'undefined'.” 对象可能是“未定义” ts2532 - object is possibly "undefined" ts2532 错误 TS2532:对象可能是“未定义” - 使用“?” 操作员 - error TS2532: Object is possibly 'undefined' - Using "?." operator Ts2532、Object 可能是“未定义” - Ts2532, Object is possibly 'undefined' Typescript 显示“this”错误 Object 说 TS2532: Object 在 vue 方法中可能是“未定义” - Typescript displays error for "this" Object saying TS2532: Object is possibly 'undefined' inside of vue methods 类型保护错误TS2532后,可能未定义Typescript对象 - Typescript Object is possibly undefined after type guard error TS2532 TS2532:对象可能是“未定义”。 在 array.map() 上 - TS2532: Object is possibly 'undefined'. on array.map() 在另一个函数中使用时,变量可能未定义(打字稿“错误TS2532”) - Variable possibly undefined when used inside another function (Typescript “error TS2532) 打字稿-内联未定义检查不起作用(对象可能是&#39;undefined&#39;.ts(2532)) - Typescript - Inline undefined check not working (Object is possibly 'undefined'.ts(2532))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM