Roboroads
Roboroads

Reputation: 1709

Typescript prepended union template literal types

I there a way to create literal string types from other string types with a prefix? Beter knowing there isn't than dwendle on this idea.

type UserField = "id" | "name";
type PostField = "id" | "message" | ("author." + UserField); // Something like this?
let somePostFields: PostField[] = ["id", "message", "author.name"];

Upvotes: 1

Views: 193

Answers (1)

Woohaik
Woohaik

Reputation: 1354

You can use the feature template literal types using string interpolation allowing union types to combine.

type UserField = "id" | "name";
type PostField = "id" | "message" | `author.${UserField}`; 

Upvotes: 1

Related Questions