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.

[V3] Support template literals

See original GitHub issue

Hello 👋

I recently encountered a case, where my schema returned a string, but a function accept only a TS template string

Here is an example showing the issue: https://codesandbox.io/s/snowy-river-n2ih2?file=/src/index.ts

image

Is there a way, currently or in the future, to be able to do this:

const str = z.template(`${z.number()}.${z.number()}.${z.number()}`)
type Str = z.infer<typeof str>
// => Str: `${number}.${number}.${number}`

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:23
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
colinhackscommented, May 2, 2021

Good idea! I definitely want to support this eventually.

For now, you’ll have to implement this with z.custom:

const literal = z.custom<`${number}.${number}.${number}`>((val) =>
  /^\d+\.\d+\.\d+$/g.test(val as string)
);
15reactions
alexburnercommented, Jul 4, 2022

Stale Bot, no! We still care ❤️

Read more comments on GitHub >

github_iconTop Results From Across the Web

"template literals" | Can I use... Support tables for ... - CanIUse
Template literals are string literals allowing embedded expressions using backtick characters (`). You can use multi-line strings and string interpolation ...
Read more >
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 >
8. Template literals - Exploring JS
Template literals are string literals with support for interpolation and multiple lines. Tagged template literals (short: tagged templates): are function calls ...
Read more >
JavaScript Template Literals - W3Schools
Back-Tics Syntax · Quotes Inside Strings · Multiline Strings · Interpolation · Variable Substitutions · Expression Substitution · HTML Templates · Browser Support....
Read more >
Documentation - Template Literal Types - TypeScript
When used with concrete literal types, a template literal produces a new string literal type by concatenating the contents. ts. type World =...
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