const TemplateStringsArray for TaggedTemplateExpression
See original GitHub issueSearch Terms
TemplateStringsArray, TaggedTemplateExpression
Suggestion
There shouldn’t be a type TemplateStringsArray
, it should be a const string tuple type
, so we can write code:
interface SQL<TSA, VS> {
texts: TSA;
values: VS;
}
function sql<TSA extends readonly string[], VS extends any[]>(texts:TSA, ...values: VS): SQL<TSA, VS> {
return { texts, values };
}
// then
let s: SQL<['select * from person where a=', ' and b=', ''], [number, Date]> = sql`select * from person where a=${1} and b=${new Date()}`;
Use Cases
Just like in the above code, we can test the sql at compile time or use a TypeScript Language Service Plugin (in fact, I’m writing itts-sql-plugin and then come across this problem).
Examples
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
There is another issue is alike. https://github.com/microsoft/TypeScript/issues/16552#issuecomment-492919476
Besides, TemplateStringsArray
is ReadonlyArray<string>
, it should be const anyway.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:40
- Comments:23 (2 by maintainers)
Top Results From Across the Web
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >Eloy Durán on Twitter: "Guess not https://t.co/R5MWSh0WaS" / Twitter
const TemplateStringsArray for TaggedTemplateExpression · Issue #31422 ... shouldn't be a type TemplateStringsArray, it should be a const string tuple type, ...
Read more >Infer Types from Tagged Template Literal Function argment
It just read type as TemplateStringsArray. const tag = <T extends ReadonlyArray<string>>(template: T, ...args ...
Read more >Package @pulumi/pulumi | Node.js SDK
Constant to represent the 'root stack' resource for a Pulumi application. The purpose of this is solely to make it easy to write...
Read more >https://unpkg.com/byots@4.3.0-dev.20210224.17.12/b...
const assertDefined: typeof checkDefined; function assertEachIsDefined<T extends ... NodeArray<Expression>; } export interface TaggedTemplateExpression ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Dear TS team, I’d like to reiterate:
Currently, the above is not possible.
Any thoughts would be greatly appreciated!
+1 for this feature. I have a case where I’m writing typings for a Flow library, where we could have safer typings thanks to template literal types, except the pattern the library uses is to have a tagged template function. I can’t really change their pattern.
The example can be found here, where
graphqlTag
doesn’t work.