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.
This commit is contained in:
Eli Barzilay
2021-04-09 04:47:02 -04:00
parent 7aacd6b274
commit 0454ae4720
5 changed files with 37 additions and 4 deletions

View File

@@ -487,11 +487,13 @@ interface StringConstructor {
fromCodePoint(...codePoints: number[]): string;
/**
* String.raw is intended for use as a tag function of a Tagged Template String. When called
* as such the first argument will be a well formed template call site object and the rest
* parameter will contain the substitution values.
* String.raw is usually used as a tag function of a Tagged Template String. When called as
* such, the first argument will be a well formed template call site object and the rest
* parameter will contain the substitution values. It can also be called directly, for example,
* to interleave strings and values from your own tag function, and in this case the only thing
* it needs from the first argument is the raw property.
* @param template A well-formed template string call site representation.
* @param substitutions A set of substitution values.
*/
raw(template: TemplateStringsArray, ...substitutions: any[]): string;
raw(template: { raw: readonly string[] | ArrayLike<string>}, ...substitutions: any[]): string;
}

View File

@@ -0,0 +1,6 @@
//// [stringRawType.ts]
String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2);
//// [stringRawType.js]
String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2);

View File

@@ -0,0 +1,7 @@
=== tests/cases/compiler/stringRawType.ts ===
String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2);
>String.raw : Symbol(StringConstructor.raw, Decl(lib.es2015.core.d.ts, --, --))
>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)
>raw : Symbol(StringConstructor.raw, Decl(lib.es2015.core.d.ts, --, --))
>raw : Symbol(raw, Decl(stringRawType.ts, 0, 12))

View File

@@ -0,0 +1,15 @@
=== tests/cases/compiler/stringRawType.ts ===
String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2);
>String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2) : string
>String.raw : (template: { raw: readonly string[] | ArrayLike<string>; }, ...substitutions: any[]) => string
>String : StringConstructor
>raw : (template: { raw: readonly string[] | ArrayLike<string>; }, ...substitutions: any[]) => string
>{ raw: ["foo", "bar", "baz"] } : { raw: string[]; }
>raw : string[]
>["foo", "bar", "baz"] : string[]
>"foo" : "foo"
>"bar" : "bar"
>"baz" : "baz"
>1 : 1
>2 : 2

View File

@@ -0,0 +1,3 @@
// @target: es6
String.raw({ raw: ["foo", "bar", "baz"] }, 1, 2);