flow icon indicating copy to clipboard operation
flow copied to clipboard

Support predicate types

Open sophiebits opened this issue 12 years ago • 67 comments

Currently, if you do

if (typeof x === "function") {
  // ...
}

then Flow knows that the code within the if statement is a function. This doesn't work if you instead call a helper function like Underscore's _.isFunction. Similarly, utilities like Object.assign and React.PropTypes seem to have hardcoded behavior at the AST level, so they don't work when aliased or given alternative names.

Will it be possible to teach Flow about these sorts of functions so that it's possible to use these third-party libraries without changing code too much?

sophiebits avatar Nov 18 '14 23:11 sophiebits

Certainly we could hardcode this stuff in the typechecker, but I would love to come up with syntax to declare this kind of knowledge. We performed this kind of thought experiment with Hack without much success.

So for an isFunction(x) test, it would be great if we could declare that, if isFunction(x) returns true, then x is a function. Otherwise it is not a function. So maybe something like

declare function isFunction<T>(T x): true when T = function, false otherwise;

But that's kind of verbose. Maybe overloading can help?

declare function isFunction(x: function): true;
declare function isFunction(x: not function): false;

Ideally you'd also be able to declare when certain functions will throw, like in an assertFunction(x) function

declare function assertFunction<T extends function>(x: T): T;
declare function assertFunction(x: not function): explodes;

gabelevi avatar Nov 18 '14 23:11 gabelevi

I just make problems; I don't fix them. :) I agree this probably isn't super trivial to fix.

sophiebits avatar Nov 18 '14 23:11 sophiebits

The way this is tackled in things like core.typed is through a shorthand syntax for predicates on types. For example (ann function? (Pred Function)) defines a boolean return type with type refinement implications for the function? declaration. The full annotation would be along the lines of:

(ann function? (All [x] (IFn [x -> Boolean
                                   :filters {:then (is Function 0) :else (! Function 0)}])))

Where 0 refers to the first argument (x), and specifies refinements in each branch based on the return value. I don't think you'd ever get annotations like this to be particularly clean, though making them available certainly reduces the amount of special casing/privileged forms.

(I'm not really sure how you'd translate this kind of syntax to Flow, though having functions from types-to-types would seem to be valuable, so that declare function isFunction<T>(T x): Predicate(function); could return the properly annotated Boolean return type)

c-spencer avatar Nov 19 '14 08:11 c-spencer

TypeScript developers and users are currently discussing a similar feature in issue 1007

spion avatar Nov 20 '14 11:11 spion

On a related note, since:

typeof null === 'object'

constraining types with code such as:

if (typeof o === 'object') {
    o.someProperty;
}

is not sufficient, whereas a simple _.isObject(o) would be.

I was actually really surprised Flow didn't catch the null issue I had in code such as the above, and only found it during testing.

jareware avatar Dec 16 '14 15:12 jareware

It looks like TypeScript ultimately decided on predicate function syntax, should flow try to use the same syntax?

function isB(p1: any): p1 is B;
(p1: any) => p1 is B;

Where the syntax in the return type could be Type | Predicate

Where Predicate := Identifier "is" Type

leebyron avatar Jul 30 '15 23:07 leebyron

@leebyron Cool! Can you provide a link to this discussion on TypeScript's side of things?

It's possible that Flow might have different capabilities and constraints around refinements that could affect the syntax here. For example, refinements can be anded and ored together, and flow can refine specific keys within object types.

Also, it seems like a function would always carry a (maybe void) return type, and optionally a refinement. Not either/or. For example, _.isFunction(x) still returns a boolean, but if the return value is true, x is an array. If it's false, x is not an array, and flow can use that information, too.

samwgoldman avatar Jul 31 '15 00:07 samwgoldman

https://github.com/Microsoft/TypeScript/issues/1007 is the issue discussing this, implemented in https://github.com/Microsoft/TypeScript/pull/3262.

https://github.com/Microsoft/TypeScript/pull/3262/files#diff-b09312f5c227031dfe4692c34364e2f0R16 this file is full of parser test cases.

leebyron avatar Jul 31 '15 00:07 leebyron

TypeScript's behavior seems to be that functions which return these type predicate refinements actually always return booleans.

I would propose that within the function, flow actually checks to ensure any possible "true" return agrees with the annotated type refinement.

Example, this should typecheck:

function isFunction(value: any): value is Function {
  if (typeof value === 'function') {
    return true;
  }
  return false;
}

var value: any = () => {};
if (isFunction(value)) {
  value();
}

This should fail:

function isFunction(value: any): value is Function {
  if (typeof value === 'function') {
    return true;
  }
  // Error: isFunction must return boolean
}

var value: any = () => {};
if (isFunction(value)) {
  value();
}

And this should fail:

function isFunction(value: any): value is Function {
  return true; // flow doesn't think "value" is Function here.
}

var value: any = () => {};
if (isFunction(value)) {
  value();
}

leebyron avatar Jul 31 '15 00:07 leebyron

Yeah, it seems like the x is y syntax is a boolean type with additional information (predicates that hold when the boolean is true).

This system is a good 80% solution, I think. We wouldn't be able to annotate the type of invariant, for example, because it returns void or blows up. I think that's OK.

I agree that flow should check that the return type is boolean, and it should also check that the truthiness of the returned value entails the correct refinement.

So, this would be bad:

function isFunction(x: any): x is Function {
  if (typeof value === 'function') {
    return false; // backwards
  }
  return true;
}

samwgoldman avatar Jul 31 '15 00:07 samwgoldman

Totally, flow should catch that backwards predicate.

This system is a good 80% solution, I think. We wouldn't be able to annotate the type of invariant, for example, because it returns void or blows up. I think that's OK.

Yeah, I think this is okay too. Invariant does something a little bit different anyhow, it may make sense to be a different scope of work.

...
invariant(isFunction(value));
value();

flow should be able to type check this correctly as well.

leebyron avatar Jul 31 '15 00:07 leebyron

Ah, it would also be very cool if we could infer a proof-carrying boolean. Consider this trivial example.

function isNull(val) { return val == null }

function safeFormatNumber(x: ?number) {
  if (isNull(x)) {
    return "";
  } else {
    return x.toFixed(2); // no error
  }
}

We didn't annotate isNull as carrying a refinement, but Flow could infer it. Maybe this is obvious behavior, but I don't think anyone has spelled it out yet :)

samwgoldman avatar Jul 31 '15 18:07 samwgoldman

That's a great point. I imagine that would work similarly to flow's current return type annotation rules. If your isNull function is inline to the file, hopefully flow would just figure that out for you. If you export function isNull then you should be required to annotate.

leebyron avatar Jul 31 '15 18:07 leebyron

I'm still interested in this feature, and it'd be great if @gabelevi could chime in about whether we want to move forward with this syntax. If we do, we'd need to build in support with Babel to strip the type. Currently x is y in the type position is a parse error.

samwgoldman avatar Aug 13 '15 18:08 samwgoldman

It would be interesting if there were some way for the refinements to be associated across multiple functions. For example a has/get scenario:

class Wrapper<V> {
  ...
  get(): ?V {
    return this._v;
  }
  has(): boolean {
    return this._v != null;
  }
}
unwrap(wrapper: Wrapper<string>): string {
  return wrapper.has() ? wrapper.get() : 'default_value';
}

kyldvs avatar Apr 20 '16 00:04 kyldvs

For me will be nice following syntax:

function isNeededThing(aaa: predicate): boolean {
  if(...) {
    aaa: SomeType = aaa;
    return true;
  }
  return false;
}

nodkz avatar Aug 10 '16 20:08 nodkz

Right now at work and I can give more clarification.

More complex example:

function isNeededThing(testObject: some<T1 | T2 | T3>): boolean {
  if(...) {
    testObject: T2 = testObject;
    return true;
  } else if (...) {
    testObject: T1 = testObject;
    return true;
  }
  testObject: T3 = testObject;
}

if (isNeededThing(someObj)) {
   // here someObj may became one of the types  T1 | T2
} else {
   // here someObj became T3
}

I offer to add some keyword. It is new type likes mixed, but flow pointed that this type MAY be described below via self-assignment. Eg. testObject: T1 = testObject;, which have no sense for JS, and may be stripped out by optimizers.

In generally self-assignment gives the missing stuff. And it pins type of the variable from its definition to down in the scope.

And some keyword points to export derived type to upper scope.

Another example, only with self-assignment:

const callbackList: FunctionT[] = [];
if (isFunction(cb)) {
  cb: FunctionT = cb;
  callbackList.push(cb);
}

nodkz avatar Aug 11 '16 09:08 nodkz

@nodkz If I'm understanding your examples right, then I don't think that idea adds anything that the x is Type-return type idea couldn't do. You could possibly write isNeededThing this way instead:

function isNeededThing(testObject: T1|T2|T3): testObject is T1|T2 {
  return testObject instanceof T1 || testObject instanceof T2;
}

if (isNeededThing(someObj)) {
   // here someObj is T1 | T2
} else {
   // here someObj is T3
}

Also consider that your way can't have the function be described from a declaration, unlike the x is Type-return type idea. There's not enough information here to be usable:

declare function isNeededThing(testObject: some<T1 | T2 | T3>): boolean;

Macil avatar Aug 11 '16 18:08 Macil

I really like this proposal, and read all the comments but may not understand everything, so apologies for stupid things in advance 😁

Best would ofcourse be to have it infer it automatically, but if that doesn't always work out (and for other things like exits or errors?), what about making special wrapper types for that that work with Tagged Unions?

declare function isObject<T>(value: T): $TypeCast<T: Object, true> | $TypeCast<T: any, false>;

where TypeCast's definition would be something like $TypeCast< (an actual type cast that is true when this type is returned ), (the type that it is bound to) >. This would benefit even more from a negate-type, that would allow to communicate something like "If this type is returned, T is not an Object"

This would also make it (maybe something-ish) possible to have invariant working like

declare function invariant(predicate: $Throw<Error, false> | true): void;

although I am not sure if the $Throw in the arguments are clear enough that it could return it... also would need a Truthy or Falsy type to fully describe invariant :-/

And process.exit would be

...
declare function exit(status: number): $Exit<void>;
...

EDIT: Actually, Falsy is just type Falsy = '' | null | void | 0 | false; (Okay without NaN)

EDIT EDIT: Invariant could also type it's effect in the return value using overloading

declare function invariant(predicate: Falsy, message: string): $Throw<Error, void>;
declare function invariant(predicate: any, message: string): void;

dralletje avatar Aug 14 '16 14:08 dralletje

What is the state of this? :)

montogeek avatar Aug 30 '16 19:08 montogeek