为什么我不能直接为数字属性赋值null,但如果它是if语句的结果我可以吗?

时间:2018-03-19 19:24:06

标签: typescript

为什么TypeScript对我这样做没有问题:

return {
 Price: value.rawPrice === null ? null : value.rawPrice
}

但我这样做有问题:

return {
 Price: null
}

字段Price的类型编号为

2 个答案:

答案 0 :(得分:0)

这是因为编译器中的var balls = []; var obstacle; function setup() { createCanvas(windowWidth, windowHeight); obstacle = new Obstacle(); } function draw() { background(75); obstacle.display(); for(var i = 0; i < balls.length; i++) { balls[i].display(); balls[i].update(); balls[i].edges(); console.log(RectCircleColliding(balls[i], obstacle)); } } function mousePressed() { balls.push(new Ball(mouseX, mouseY)); } function Ball(x, y) { this.x = x; this.y = y; this.r = 15; this.gravity = 0.5; this.velocity = 0; this.display = function() { fill(255, 0 , 100); stroke(255); ellipse(this.x, this.y, this.r*2); } this.update = function() { this.velocity += this.gravity; this.y += this.velocity; } this.edges = function() { if (this.y >= windowHeight - this.r*2) { this.y = windowHeight - this.r*2; this.velocity = this.velocity* -1; this.gravity = this.gravity * 1.1; } } } function Obstacle() { this.x = windowWidth - windowWidth; this.y = windowHeight / 2; this.w = 200; this.h = 25; this.display = function() { fill(0); stroke(255); rect(this.x, this.y, this.w, this.h); } } function RectCircleColliding(Ball, Obstacle) { // define obstacle borders var bRight = Obstacle.x + Obstacle.w; var bLeft = Obstacle.x; var bTop = Obstacle.y; var bBottom = Obstacle.y + Obstacle.h; //compare ball's position (acounting for radius) with the obstacle's border if(Ball.x + Ball.r > bLeft) if(Ball.x - Ball.r < bRight) if(Ball.y + Ball.r > bTop) if(Ball.y - Ball.r < bBottom) return(true); return false; }

答案 1 :(得分:-1)

使用 undefined 代替 null

here a working example