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 have a function which takes two string literals, a namespace
and a name
, and combines these with a / in between as it's output, but I can't figure out a way to make the output a string literal and not a generic string.
I need it to be a string literal because the output will be used as a key of an object.
I've tried type intersections(&
), +
, .concat()
function makeKey<NS extends string, N extends string>(namespace: NS, name: N) {
return namespace + '/' + name; // <- want this to be `NS + / + N` = `NS/N`
}
// I want this to return a string literal rather than a generic string
const objKey = makeKey('admin', 'home')
// I want typeof objKey to be a string literal: `"admin/home"`, not a generic `string`
typeof objKey
is a generic string
but I want it to be a string literal
"admin/home"