我在打字本里有这段代码
/**
* Team information
*/
export class Team {
constructor( public name: string ) {}
}
/**
* Half side of each match, with team and score of the team
*/
export class SideMatch {
constructor( public team: Team, public score: number) {}
}
/**
* Two side matches, that represents the match itself
*/
export class Match {
constructor( private game: SideMatch[] ) { }
addScore(team: number, goal: number) {
this.game[team].score += goal;
}
winner() : number {
if( this.game[0].score > this.game[1].score )
return 0;
if( this.game[0].score < this.game[1].score )
return 1;
return -1;
}
getGame() {
return this.game;
}
toString() {
console.log( `${this.game[0].team.name} ${this.game[0].score} vs ${this.game[1].score} ${this.game[1].team.name}` );
}
}
/**
* Team stats, with points, (todo: victories, draws, loses, goals forward, goals against, goals difference)
*/
export class TeamStats {
constructor(public team: Team, public point: number) {}
}
export class Tournament {
private teams: Team[] = [];
private table: TeamStats[] = [];
private fixtures: Match[] = [];
constructor() {}
public addTeam(teamName: string) {
let team: Team = new Team(teamName);
this.teams.push(team);
console.log(this.teams);
let teamStats = new TeamStats(team, 0);
this.table.push(teamStats);
}
/**
* Get the team
* #question how to return only the Team type? It's requiring to define undefined
* @param teamName
* @returns
*/
public getTeam(teamName :string) : Team {
try {
//let teamFound = new Team('temp');
const teamFound = this.teams.find(t => t.name = teamName);
console.log(`team found ${teamFound.name}`);
if (teamFound === undefined) {
throw new TypeError('The value was promised to always be there!');
}
return teamFound;
} catch (error) {
throw error;
}
}
public addMatch(team1: Team, team2: Team) {
let sideMatch1: SideMatch = new SideMatch(team1, 0);
let sideMatch2: SideMatch = new SideMatch(team2, 0);
console.log(`add match - sm1 ${sideMatch1.team.name}` );
console.log(`add match - sm2 ${sideMatch2.team.name}` );
var game1: SideMatch[] = [];
game1.push(sideMatch1);
game1.push(sideMatch2);
console.log(game1);
let match1 = new Match( game1 );
this.fixtures.push(match1);
}
public addMatchResults(matchIndex: number, score1: number, score2: number) {
this.fixtures[matchIndex].addScore(0, score1);
this.fixtures[matchIndex].addScore(1, score2);
this.calculateMatchPoints(matchIndex);
}
private calculateMatchPoints(matchIndex: number) {
let results : number = this.fixtures[matchIndex].winner();
console.log(results);
if (results !== -1)
{
console.log(this.fixtures[matchIndex].getGame());
}
}
public getMatch(index: number) : Match {
return this.fixtures[index];
}
}
当我尝试在CLI中使用
tsc src/compile.ts
它显示以下错误:
D:\src\myApps\worldcup>tsc src/console
src/console.ts:80:42 - error TS2550: Property 'find' does not exist on type 'Team[]'. Do you need to change your target library? Try changing the 'lib' compiler option
to 'es2015' or later.
80 const teamFound = this.teams.find(t => t.name = teamName);
~~~~
Found 1 error in src/console.ts:80
所以我在tsconfig.json中包含了这些设置:
{
"compileOnSave": false,
"compilerOptions": {
"strictNullChecks": false,
"lib": [
"es2020",
"dom",
]
},
}
现在我有两个问题:
1.我不能只使用tsc src/console.ts来翻译。我现在需要使用tsc -B。为什么?
1.当我使用这些函数运行代码时,当我直接使用getTeam以及将getTeam方法的结果赋给变量时,会发生一些奇怪的事情:
let worldCup2022: Tournament = new Tournament();
worldCup2022.addTeam('Brazil');
worldCup2022.addTeam('Canada');
console.log('t1 name with getTeam ' + worldCup2022.getTeam('Canada').name); // shows correctly t1 name Canada
console.log('t2 name with getTeam ' + worldCup2022.getTeam('Brazil').name); // shows correctly t2 name Brazil
const team1 = worldCup2022.getTeam('Canada');
const team2 = worldCup2022.getTeam('Brazil');
console.log('t1 name ' + team1.name); // shows incorrectly t1 name as Brazil (last one got)
console.log('t2 name ' + team2.name); // shows incorrectly t1 name as Brazil (last one got)
1条答案
按热度按时间ua4mk5z41#
TSC问题
你在根文件夹中运行过tsc -init吗?这将初始化一个tsconfig文件,然后你可以运行
运输安全委员会
它会编译所有的根目录。我的项目结构是这样的:
File Structure
团队名称问题
查找团队的问题是您要使用= in检查相同的类型
当你真正想比较价值时:
= vs == vs ===