Skip to content

Inconsistency requiring explicit casts on nullable value checked for null #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andy-hanson opened this issue Aug 27, 2018 · 2 comments
Closed

Comments

@andy-hanson
Copy link
Contributor

class Point {
	constructor(readonly x: f64, readonly y: f64) { this.x = x; this.y = y; }
	public sum(): f64 {
		return this.x + this.y;
	}
}

function maybe_point(x: f64, y: f64): Point | null {
	return x + y === 0 ? null : new Point(x, y);
}

function sum(p: Point): f64 {
	return p.x + p.y;
}

export function test(a: f64, b: f64): f64 {
	let p = maybe_point(a, b);
	let s0 = p !== null ? p.sum() : 0;
	let s1 = p !== null ? sum(p) : 0;
	return s0 + s1;
}

There is no error when compiling with TypeScript. But when compiling with asc there is an error:

ERROR AS200: Conversion from type 'Point | null' to 'Point' requires an explicit cast.

 	let s1 = p !== null ? sum(p) : 0;

Ideally there would be no such errors since TypeScript can check that p is non-null at that location. This works for s0 so I would expect it to also work for s1.

@dcodeIO
Copy link
Member

dcodeIO commented Aug 27, 2018

Yeah, null checking in flows is still todo :)

@MaxGraey
Copy link
Member

Closed in favour of #456

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants