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

encaps error when tag template returns object #579

Closed
brigand opened this issue Jun 27, 2015 · 4 comments
Closed

encaps error when tag template returns object #579

brigand opened this issue Jun 27, 2015 · 4 comments
Assignees

Comments

@brigand
Copy link

brigand commented Jun 27, 2015

This is the minimal:

function tag(strings) {
    return {};
}

var result = tag`foo`;

Gives:

/private/tmp/temp-27799/test.js:6:14,21: encaps tag
Error:
/private/tmp/temp-27799/test.js:3:12,13: object literal
This type is incompatible with
/private/tmp/temp-27799/test.js:6:14,21: string

Found 1 error

In the actual situation it's returning a well typed object.

The only way to avoid the error I've found is to declare the function's return type and the variable's type explicitly to any.

@samwgoldman
Copy link
Member

Is it valid to return anything other than a string from a tagged template?

@samwgoldman
Copy link
Member

If so, this should be pretty simple to fix. Tagged template expressions currently assume the tag function returns a string and the expression itself is a string. Reference Type_inference_js.ml.

@brigand
Copy link
Author

brigand commented Jun 27, 2015

Yeah it's just a CallExpression, the only special part is the arguments it receives.

The most permissive type for a function used as a template tag:

f(strings: (Array<string>) & ({raw: Array<string>}), ...args: any)

Edit: flow seems to not like that type definition

Also strings and strings.raw are frozen, and strings[0] and strings.raw[0] are guaranteed to be strings.

@samwgoldman
Copy link
Member

Thanks for the details! Unfortunately, array&object intersections don't currently do what you expect, but I think that's incompleteness, not intentional.

Flow also doesn't know what a frozen array is, for a similar issue (arrays are pretty different from objects in flow, but are objects in JS).

For now, though, the associated PR lets you return non-string values. I'll make sure we track those other two constraints in #560.

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

Successfully merging a pull request may close this issue.

2 participants