Skip to content

Commit 0454ae4

Browse files
committed
Loosen up the first argument type for String.raw
The [String.raw spec](https://tc39.es/ecma262/#sec-string.raw) uses just the `raw` property of its first argument, which is a useful way of using it in user-defined tag functions to do the work of interleaving strings and values as well as converting the values to strings. Fixes #43609.
1 parent 7aacd6b commit 0454ae4

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

src/lib/es2015.core.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,13 @@ interface StringConstructor {
487487
fromCodePoint(...codePoints: number[]): string;
488488

489489
/**
490-
* String.raw is intended for use as a tag function of a Tagged Template String. When called
491-
* as such the first argument will be a well formed template call site object and the rest
492-
* parameter will contain the substitution values.
490+
* String.raw is usually used as a tag function of a Tagged Template String. When called as
491+
* such, the first argument will be a well formed template call site object and the rest
492+
* parameter will contain the substitution values. It can also be called directly, for example,
493+
* to interleave strings and values from your own tag function, and in this case the only thing
494+
* it needs from the first argument is the raw property.
493495
* @param template A well-formed template string call site representation.
494496
* @param substitutions A set of substitution values.
495497
*/
496-
raw(template: TemplateStringsArray, ...substitutions: any[]): string;
498+
raw(template: { raw: readonly string[] | ArrayLike<string>}, ...substitutions: any[]): string;
497499
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [stringRawType.ts]
2+
String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2);
3+
4+
5+
//// [stringRawType.js]
6+
String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/stringRawType.ts ===
2+
String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2);
3+
>String.raw : Symbol(StringConstructor.raw, Decl(lib.es2015.core.d.ts, --, --))
4+
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --) ... and 1 more)
5+
>raw : Symbol(StringConstructor.raw, Decl(lib.es2015.core.d.ts, --, --))
6+
>raw : Symbol(raw, Decl(stringRawType.ts, 0, 12))
7+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/stringRawType.ts ===
2+
String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2);
3+
>String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2) : string
4+
>String.raw : (template: { raw: readonly string[] | ArrayLike<string>; }, ...substitutions: any[]) => string
5+
>String : StringConstructor
6+
>raw : (template: { raw: readonly string[] | ArrayLike<string>; }, ...substitutions: any[]) => string
7+
>{ raw: ["foo", "bar", "baz"] } : { raw: string[]; }
8+
>raw : string[]
9+
>["foo", "bar", "baz"] : string[]
10+
>"foo" : "foo"
11+
>"bar" : "bar"
12+
>"baz" : "baz"
13+
>1 : 1
14+
>2 : 2
15+

tests/cases/compiler/stringRawType.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @target: es6
2+
3+
String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2);

0 commit comments

Comments
 (0)