(I have a reason for using a separate declare statement to have the %checks on instead of putting it right on the actual function, but that's not important now.)
My problem is that I don't seem to have a way to write a type refinement function for an opaque alias type?
For example, I use SHA-256 hashes in my code, which are strings, but very special strings (obviously). So I'm using an alias
export opaque type SHA256Hash: string = string;
It's very unfortunate that I can't just tell Flow that a function checks a given type, that I can only use typeof and instanceof. That only works for "fake OOP" class-based programming where instanceof works. It doesn't work for a functional style when I have just different kinds of plain objects and no inheritance and nothing is constructed using new. And plain types cannot be aliased if only typeof is allowed for refinement.
I'm using opaque alias types described here: https://flow.org/en/docs/types/opaque-types/
I'm also using the undocumented
checks%
hack to define type refinement functions, for exampleand the actual function is defined as
(I have a reason for using a separate
declare
statement to have the%checks
on instead of putting it right on the actual function, but that's not important now.)My problem is that I don't seem to have a way to write a type refinement function for an opaque alias type?
For example, I use SHA-256 hashes in my code, which are strings, but very special strings (obviously). So I'm using an alias
It's very unfortunate that I can't just tell Flow that a function checks a given type, that I can only use
typeof
andinstanceof
. That only works for "fake OOP" class-based programming whereinstanceof
works. It doesn't work for a functional style when I have just different kinds of plain objects and no inheritance and nothing is constructed usingnew
. And plain types cannot be aliased if onlytypeof
is allowed for refinement.