Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
This can be done using conditional types. I defined these helper types: enum Role { server, client }; type PossiblyHiddenFromClients<R extends Role, T> = T | (R extends Role.client ? un...
Answer
#1: Initial revision
This can be done using [conditional types](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html). I defined these helper types: ```typescript enum Role { server, client }; type PossiblyHiddenFromClients<R extends Role, T> = T | (R extends Role.client ? undefined : never); ``` When R is `server`, this type reduces to `T` (via `T | never`); when R is `client`, it is `T | undefined`. `User` can then be defined as: ```typescript type User<R extends Role> = { name: string; superSecretGovernmentIdNumber: PossiblyHiddenFromClients<R, string>; }; ``` There are now two types, `User<Role.server>` representing a full unredacted user, and `User<Role.client>` representing one that may or may not have information missing. The client should always use the latter; the server uses the former internally and converts to the latter (redacting as required) when returning data to the client.