Wird geladen…
Wird geladen…
How to use Templace Literal Types to increase safety and reduce work.

How to use Templace Literal Types to increase safety and reduce work.
We have some function the converts a camelCased string, let's say "teamOneDevelopers" into a kebab-cased one: "team-one-developers" and we want a type to represent the output based on the input string. So our outcome should look something like this:
function camelCaseToKebabCase(input: T): CamelCaseToKebabCase {
...
}In order to convert a camelCased string to a kebab-cased one we need to find all uppercased letters and replace them with a minus followed by the lowercased version of the letter. By infering parts of the string like below, we can get the type of the first letter of a string, followed by the rest. This should always be possible, unless we have and empty string, so we can just return string in that case.
type CamelCaseToKebabCase = V extends `${infer A}${infer Rest}`
?
: stringIn the case that we do have a letter we need to check if it is uppercase. If it is not a uppercased letter, we can leave it as it is and run the rest of the string through our type again.
type CamelCaseToKebabCase = V extends `${infer A}${infer Rest}`
? A extends Uppercase
?
: `${A}${CamelCaseToKebabCase}`
: stringIn the case that we hit an uppercased letter we need to replace it by a minus sign and the lowercased version of it. Afterwards, we still need to run the rest of the string through our type again.
type CamelCaseToKebabCase = V extends `${infer A}${infer Rest}`
? A extends Uppercase
? `-${Lowercase}${CamelCaseToKebabCase}`
: `${A}${CamelCaseToKebabCase}`
: string;
We are almost done, we just need to fix one minor mistake. When this recursive type reaches the last letter of the string, it calls itself with the rest again. This rest is now just an empty string. In that case we want to return an empty string again. This now solves our problem.
type CamelCaseToKebabCase = V extends ""
? ""
: V extends `${infer A}${infer Rest}`
? A extends Uppercase
? `-${Lowercase}${CamelCaseToKebabCase}`
: `${A}${CamelCaseToKebabCase}`
: string;



Erfolgreiche Produkte entstehen nur, wenn die echten Bedürfnisse der Nutzer:innen verstanden werden. Product Discovery hilft Teams, Probleme präzise zu identifizieren, Ideen früh zu testen und Lösungen zu entwickeln, die echten Mehrwert schaffen.


If you use Git via the terminal, here's a quick tip that might make your workflow easier.


Having a single digital place to manage my personal knowledge.