Skip to content
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

Support for typeof aliasing #2339

Closed
wessberg opened this issue Aug 26, 2016 · 3 comments
Closed

Support for typeof aliasing #2339

wessberg opened this issue Aug 26, 2016 · 3 comments

Comments

@wessberg
Copy link
Contributor

Consider the following scenario:

type MyUnionType = string|number;

function isString (str: any): boolean {
    return typeof str === "string";
}

function myFunction (): MyUnionType {
    if (something) return "a string";
    else return 0;
}
let result = myFunction();
if (isString(result))           str.length; // flow warning
if (typeof result === "string") str.length; // All okay!

It would be preferable if flow understood that we already did the type check. For instance, consider a scenario where type typeof selector simply isn't enough:

// Checks if the string is a primitive or Object string.
function isStringType (str: any): boolean {
    return typeof str === "string" || str instanceof String;
}

We could solve it by writing:

if (typeof str === "string" || str instanceof String) str.length   // All okay!

But that's pretty boilerplatey and screams for a helper method.
So, my request is:

  • How about making it possible to declare via a flow comment that the return value of a function substitutes the typeof/instanceof expressions?
  • Or, how about parsing the return values of functions to see if they actually already determine whether or not a property exists on the return value?
@aackerman
Copy link
Contributor

Sounds like a duplicate of #34

@samwgoldman
Copy link
Member

Yeah, this is a duplicate so I'm going to close in favor of that issue. @wessberg feel free to subscribe there. This is something we're actively working on so you can expect an announcement in that issue soon. :)

@montogeek
Copy link
Contributor

I'm getting this behaviour when using Lodash isString module:

declarations

declare module 'lodash.isstring' {
  declare function exports(value: any): boolean;
}

src.js

parse: (value: Object|string): Object => {
    let query: Object|string = {};

    if (isString(value)) {
      query = url.parse(value, true).query;
    }
    ...
}

Error:

 28:       query = url.parse(value, true).query;
                   ^^^^^^^^^^^^^^^^^^^^^^ call of method `parse`
 28:       query = url.parse(value, true).query;
                             ^^^^^ object type. This type is incompatible with
1224:     urlStr: string,
                  ^^^^^^ string. See lib: /private/tmp/flow/flowlib_176bd1d7/node.js:1224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants