-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Smarter control flow analysis for null check #10134
Comments
I think I'm hitting the same issue. Is this not the same as #8437 (which was 'fixed') for getters? EDIT No it's not the same issue |
What you are really asking for is some sort of guard that you can describe, otherwise how would TypeScript understand what is going without deep introspection of methods. To even have this in the realms of possibility, the return value needs to denote the guard. Also, you are also implying you are asking for a negation guard as well. Potentially, something like this: class Test {
foo: string | null;
get hasFoo(): this.foo is string {
return this.foo !== null;
}
hasFoo2(): this.foo is string {
return this.foo !== null;
}
}
function isNotNull(value): value is not null {
return value !== null;
} Related, #5101 suggests inferred type guards, which could potentially solve some if these use cases. #4183 discusses negation/subtraction types. |
Note that with hasFoo(): this is this & { foo: string } {
return this.foo !== null;
} But this has to be on a method, not a get-accessor. |
@DanielRosenwasser Is it by design that get-accessor can't use I really want do to more complex stuff like that: class FormValue<T> {
@observable value: T;
@observable error: FormError | null;
@observable isPristine: boolean = true;
@computed get isDirty(): boolean {
return !this.isPristine;
}
@computed get isValid(): boolean {
return this.error === null;
}
@computed get isInvalid(): boolean {
return !this.isValid;
}
@computed get shouldShowErrors(): boolean {
return this.isInvalid && this.isDirty;
}
}
// and later somewhere
const Input = observer((props) =>
<div>
<input type="text" value={props.formValue.value}/>
{props.formValue.shouldShowErrors ?
<p>{props.formValue.error}</p> {/* error is possibly null here */}
:
null
}
</div>
) It would be great if I could tell TypeScript:
|
TypeScript Version: 2.0.0
Code
It would be really nice, if the control flow analysis would check functions, methods and getters. This is really helpful for
strictNullChecks: true
.The text was updated successfully, but these errors were encountered: