There is no simple answer. You basically have three options.
- Limit typechecker by discarding type security.
- For type safety, run a runtime check. Flow understands many runtime checks and will refine types based on them.
- Restructure your program so that these properties are not optional.
To completely bypass typechecker and give up security, you can do something like (a: any).b.action() . I do not recommend this.
Obviously, there is not enough information in this matter to determine whether it is possible or even desirable to change the structure of your program in order to avoid the presence of additional properties.
So, to ensure type safety, you need to have a run-time check. You can do something like:
if (ab != null && abaction != null) { abaction(); } else { // throw error or something }
Or, if you just want to claim that they are not null, Flow has special functions called invariant for this purpose (of course, you need to figure out how to get it at runtime. In Node, you can do import invariant from 'assert' It's pretty easy to write yourself if you want, though).
invariant(ab != null && abaction != null); abaction();
One caveat is that Flow aggressively overrides type qualifications if it thinks something could be changed. Thus, if between testing and using any intermediate function calls, an error may occur again. In this case, you will have to pull each bit into a separate variable, for example:
const b = ab; invariant(b != null); const action = b.action; invariant(action != null);
source share