MaciejDot
MaciejDot

Reputation: 43

Typescript joining key names/ string types

Is there any way in typescript to dynamically join keys names / dynamically join string types? what i mean by that is :

type JoinString<A extends string, B extends string> = /*some magic*/

const example = {
foo: "b",
bar: "c"
}

type GetAnotherKeys<T> = {
 JoinString<[TKey in keyof T], "_">: T[Tkey]
}

type Result = GetAnotherKeys<typeof example>;
/*
Result ={
foo_:string
bar_:string
}

*/





Upvotes: 1

Views: 430

Answers (1)

Here is a simple example:

type AddPrefix<T extends string>=`${T}_`

type Keys={
   name:string;
   surname:string;
}

type MapPrefix<T>={
   [P in keyof T as AddPrefix<string & P>]:T[P]
}

type Result = MapPrefix<Keys>

More information you can find here

Upvotes: 2

Related Questions