craigpatik
craigpatik

Reputation: 969

How to define a type that accepts template strings but not literal strings

I would like to define a type that accepts only template strings, but NOT literal strings.


type TemplateOnly = /* ??? */

const foo: TemplateOnly = `hello` // allowed
const bar: TemplateOnly = 'world' // error

Is this possible?

Upvotes: 0

Views: 367

Answers (1)

Radu Diță
Radu Diță

Reputation: 14171

Template strings are not a type, but a language construct.

As such you cannot use them as a type. More over, you could use template strings, with tagged template to return whatever type you want.

Think about it like this: you could have a string created by concatenating more strings. Something like this

const a = "something" + " " + 5

You are asking to be able to make a choice, between a concatenating string and an "inline" was.

Upvotes: 2

Related Questions