Skip to content

Commit

Permalink
Tagged templates can return any type, not just strings
Browse files Browse the repository at this point in the history
Summary: Fixes #579
Closes #580

Reviewed By: @jeffmo

Differential Revision: D2198666

Pulled By: @mroch
  • Loading branch information
samwgoldman authored and facebook-github-bot-5 committed Jun 29, 2015
1 parent f98eec7 commit 276f7f4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/typing/type_inference_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3289,13 +3289,14 @@ and expression_ ~is_cond cx loc e = Ast.Expression.(match e with
let t = expression cx tag in
let reason = mk_reason "encaps tag" loc in
let reason_array = replace_reason "array" reason in
let ret = Flow_js.mk_tvar cx reason in
let ft = Flow_js.mk_functiontype
[ ArrT (reason_array, StrT.why reason, []);
RestT (AnyT.why reason) ]
(StrT.why reason)
ret
in
Flow_js.flow cx (t, CallT (reason, ft));
StrT.at loc
ret

| TemplateLiteral {
TemplateLiteral.quasis;
Expand Down
10 changes: 4 additions & 6 deletions tests/encaps/encaps.exp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ encaps.js:3:14,16: property x
Property not found in
encaps.js:2:13,13: A

encaps.js:9:10,22: encaps tag
Error:
encaps.js:7:12,12: number
This type is incompatible with
encaps.js:9:10,22: string

encaps.js:9:10,22: encaps tag
Error:
encaps.js:9:10,22: string
This type is incompatible with
encaps.js:6:11,16: number

encaps.js:16:2,7: string
This type is incompatible with
encaps.js:16:10,15: number

Found 3 errors
11 changes: 9 additions & 2 deletions tests/encaps/encaps.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
class A { }
var a = new A();
var s1 = `l${a.x}r`;
var s1 = `l${a.x}r`; // error: no prop x in A

function tag(strings,...values) {
var x:number = strings[0];
var x:number = strings[0]; // error: string ~> number
return x;
}
var s2 = tag `l${42}r`;

function tag2(strings,...values) {
return { foo: "" }; // ok: tagged templates can return whatever
}

var s3 = tag2 `la la la`;
(s3.foo: number); // error: string ~> number

0 comments on commit 276f7f4

Please sign in to comment.