1

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
}

*/





1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

thank you kind gentelmen it is real luck that that update was week ago
@MaciejDot You are welcome. You can accept this answer, this will help other people

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.