question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

const TemplateStringsArray for TaggedTemplateExpression

See original GitHub issue

Search 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:open
  • Created 4 years ago
  • Reactions:40
  • Comments:23 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
harrysolovaycommented, Dec 30, 2020

Dear TS team, I’d like to reiterate:

Tagged template library developers will want to…

  • Protect their users from supplying incompatibly-typed arguments.
  • Produce literal types which can be utilized elsewhere, outside of the tagged template.
  • Eliminate the unnecessary right and left parens from their string-accepting fns.

Currently, the above is not possible.

Any thoughts would be greatly appreciated!

9reactions
alloycommented, Mar 24, 2021

+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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found