我要解决的代码有错误。我认为它需要一个return
语句,但是我已经在forEach
循环之外,但是仍然会抛出错误:
not all the code path return the value
如何修复以下代码?
main.ts
:
private ValidateRequestArgs(str) {
let ret: boolean = true;
// here on val its throwing tslint error not all code paths return value
str.split(',').forEach((val) => {
if (!ret) {
return false;
}
if (List.indexOf(val) > -1) {
ret = true;
} else {
ret = false;
}
});
return ret;
}
答案 0 :(得分:14)
投诉是第一个if(){}缺少带有return语句的else {}块。您可以在tsconfig文件设置中禁用此行为:
def gameLoop():
gameExit = False
gameOver = False
lead_x = display_width/2
lead_y = display_height/2
lead_x_change = 0
lead_y_change = 0
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c and gameOver:
gameOver = False
lead_x = display_width/2
lead_y = display_height/2
lead_x_change = 0
lead_y_change = 0
if event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_LEFT:
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN:
lead_y_change = block_size
lead_x_change = 0
if gameOver == True:
gameDisplay.fill(white)
message_to_screen("Game over press c to play again or Q to quit",red)
else:
if lead_x <=0 or lead_x >= display_width or lead_y <= 0 or lead_y >= display_height:
gameOver = True
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay,red,[lead_x,lead_y,block_size,block_size])
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
当然您也可以添加
"noImplicitReturns": false,
但是我不建议这样做,因为forEach不应该返回例如此处所述的任何内容: What does `return` keyword mean inside `forEach` function? 或在这里: https://codeburst.io/javascript-map-vs-foreach-f38111822c0f
最好完全摆脱第一个if()。干杯
答案 1 :(得分:4)
我不确定tslint为何会抱怨,但是您可以将整件事写得更优雅:
return str.split(",").every(el => List.includes(el));
或ES6:
return str.split(",").every(el => List.indexOf(el) > -1);
答案 2 :(得分:3)
tsconfig.json
compilerOptions:{
"noImplicitReturns": false
}
答案 3 :(得分:0)
传递给forEach的函数的主体具有any -> boolean
的隐式签名,因此tslint似乎希望您更静态地对待它,并在所有代码路径上返回布尔值。
答案 4 :(得分:0)