Skip to content Skip to sidebar Skip to footer

Is There Currently Anyway To Concatenate Two Or More String Literal Types To A Single String Literal Type In Typescript Right Now?

First of all, let me make it clear that what I'm looking isn't a union type but a straight up concatenation i.e 'Hel' + 'lo' = 'Hello' but for string literal types Essentially I ha

Solution 1:

TS4.1+ answer:

You can now use template literal types to do this:

function makeKey<NSextends string, N extends string>(namespace: NS, name: N) {
    return namespace + '/' + name as`${NS}/${N}`
}

const objKey = makeKey('admin', 'home');
// const objKey: "admin/home"

Playground link


Pre TS4.1 answer:

The answer is unfortunately no. There are several feature suggestions filed in GitHub that, if implemented, might give you such functionality (microsoft/TypeScript#12754 to augment keys during mapped types, or microsoft/TypeScript#6579 to manipulate string types via regular expressions) but I don't think they are being actively worked on. I don't see anything in the roadmap about it, anyway. If you really want to see this happen, you might want to go to one of those GitHub issues and give them a đź‘Ť or describe your use case if it's particularly compelling. But I wouldn't hold my breath. Sorry!

Solution 2:

It will be possible once Template string types will be released (looks like in typescript 4.1):

function makeKey<NSextends string, N extends string>(namespace: NS, name: N) {
    return (namespace + '/' + name) as`${NS}/${N}`;
}

const objKey = makeKey('admin', 'home') // objKey is of type 'admin/home'

Playground


Template string types are the type space equivalent of template string expressions. Similar to template string expressions, template string types are enclosed in backtick delimiters and can contain placeholders of the form ${T}, where T is a type that is assignable to string, number, boolean, or bigint. Template string types provide the ability to concatenate literal strings, convert literals of non-string primitive types to their string representation, and change the capitalization or casing of string literals. Furthermore, through type inference, template string types provide a simple form of string pattern matching and decomposition.

Some examples:

typeEventName<T extendsstring> = `${T}Changed`;
typeConcat<S1extendsstring, S2extendsstring> = `${S1}${S2}`;
typeT0 = EventName<'foo'>;  // 'fooChanged'typeT1 = EventName<'foo' | 'bar' | 'baz'>;  // 'fooChanged' | 'barChanged' | 'bazChanged'typeT2 = Concat<'Hello', 'World'>;  // 'HelloWorld'typeT3 = `${'top' | 'bottom'}-${'left' | 'right'}`;  // 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'

More details and examples here

Solution 3:

It worked for me, with typescript 3.7.2, using template literal to concatenate and the as keyword to specify the output type of my function .

constaddSuffixToMyStringLiteral =
    (m: MyStringLiteral, suffix: string) => `${m}${suffix}`asMyStringLiteral;

I don't know if it a new feature of typescript or not.

Post a Comment for "Is There Currently Anyway To Concatenate Two Or More String Literal Types To A Single String Literal Type In Typescript Right Now?"