#Property prefix:

8 messages · Page 1 of 1 (latest)

glass dome
#

What i want to do is to transform an interface that has some properties to it´s equivalent but all properties should have a prefix (in my case a '$')
Obviously what i did in the playground doesn´t work - but shows what i want to do i guess:

https://www.typescriptlang.org/play/?#code/C4TwDgpgBA8gRgKwgY2AdQE4EMyQwHgBUA+KAXigG8AoKKAbQGkoBLAOygGsIQB7AMyiEoWAM5QA5ABIJAakYBdALQB+AFxCmCgNzUAvrurtgEDPyzJoAVVGmqtKGywBbCGtHAM7AOYAaB1gAJoEYEKKi7p4+-nRY3m5sAK7OcKb61BmgkFBSibYY5LCIKOjYuKb4NqbEGQD0tbn5dKIAFryJADaBjrwA7lCpUKK8rsAtPlAdLNxUUk6ukV5sfjkiwaHhi9E5cQnJqRh6QA

I think this is not possible - but it´s worth asking.
Thanks, dudie

gleaming tendonBOT
#

@glass dome Here's a shortened URL of your playground link! You can remove the full link from your message.

dude0220#0

Preview:```ts
type ObjectWrapper<T> = {
[K in keyof T as '$'+K]-?: T[K];
};

interface User {
name:string,
address:string,
age:number
}

type $user = ObjectWrapper<User>

//$user should now be something like {$name:string, $ address:string, $age:number}```

faint gazelle
#

ts doesn't do type-level runtime operators, you can use a template type instead

gleaming tendonBOT
#
chris.sophos#0

Preview:ts type ObjectWrapper<T> = { [K in keyof T & string as `$${K}`]-?: T[K] } ...

faint gazelle
#

(can't stringify a symbol, hence the & string)

#

not sure if that breaks it being a homomorphic mapping actually. might have to use a conditional instead

glass dome
#

Oh wow, i try that, thanks ❤️

#

That works. Thank you Chris!