You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Null checking does not follow through functions: Object possibly 'null' or 'undefined' #18244
functionhasValueGeneric<T>(value: T): boolean{returnvalue!==null&&value!==undefined;}functionhasValueAny(value: any): boolean{returnvalue!==null&&value!==undefined;}functiontest(value: string|null|undefined){if(hasValueGeneric(value)){returnvalue[0];// currently throws error but shouldn't}if(hasValueAny(value)){returnvalue[0];// currently throws error but shouldn't}if(value!==null&&value!==undefined){returnvalue[0];// does not throw error, as expected}}
Expected behavior:
Should follow through the function and compile.
Actual behavior:
Is not able to follow the function to the null check logic. Throws the below errors
(12,9): error TS2533: Object is possibly 'null' or 'undefined'.
(15,9): error TS2533: Object is possibly 'null' or 'undefined'.
Pretty sure this a bug and not a feature request, as it does not match standard user expectations based on reasonable extrapolation of documentation and isn't documented as known gap anywhere that I've found.
More than willing to submit a PR if I can get guidance on where and how to go about fixing. Not sure whether that is practical or not based on likely level of complexity for a fix. I have written in TypeScript full time for over a year now but have never contributed to the project before.
TS won't know if there's a type guard in your function unless you defined it, and auto type-guard generation looks like duplicate of #15265.
-function hasValueGeneric<T>(value: T): boolean {+function hasValueGeneric<T>(value: T): value is {} {
return value !== null && value !== undefined;
}
-function hasValueAny(value: any): boolean {+function hasValueAny(value: any): value is {} {
return value !== null && value !== undefined;
}
That fixed my problem but when I looked in the docs before logging the issue I didn't see anything like this would you have any objection to having this added to the docs? If not I take I would submit a PR to this repo: https://github.com/Microsoft/TypeScript-Handbook correct?
Actually looks like it is in the guide I was just in a sub section. I'm just going to close this since it seems to be not an issue with code or documentation. Sorry for taking up your time with a non-issue and thanks for the great response :)
Activity
ikatyang commentedon Sep 5, 2017
TS won't know if there's a type guard in your function unless you defined it, and auto type-guard generation looks like duplicate of #15265.
beattyml1 commentedon Sep 5, 2017
That fixed my problem but when I looked in the docs before logging the issue I didn't see anything like this would you have any objection to having this added to the docs? If not I take I would submit a PR to this repo: https://github.com/Microsoft/TypeScript-Handbook correct?
beattyml1 commentedon Sep 5, 2017
Actually looks like it is in the guide I was just in a sub section. I'm just going to close this since it seems to be not an issue with code or documentation. Sorry for taking up your time with a non-issue and thanks for the great response :)