Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexed types and template literals - prefix every key

Tags:

typescript

Take type A. Create type B by prefixing every key in A with x using Typescript's latest Template Literal Types:

type A = {
  a: string;
  b: string;
};

// Create this automatically.
type Prefixed = {
  xa: string;
  xb: string;
};

How do I do so?

like image 372
Sun Avatar asked Dec 30 '25 08:12

Sun


1 Answers

type B = { [T in keyof typeof A as `x${T}`]: A[T] }

Some helpers like Capitalize<T> exist as well.

like image 90
Sun Avatar answered Jan 01 '26 00:01

Sun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!