A common use case of making a Partial
of an object, but leaving a few keys as is.
export type OptionalExcept<Type, AsIsKeys extends keyof Type> = Pick<Type, AsIsKeys> & Partial<Omit<Type, AsIsKeys>>;
Example:
interface User {
username: string;
isAdmin: boolean;
dateCreated: Date;
}
function newUser(userOptions: OptionalExcept<User, 'username'>): User {
return {
// Default values for optionals (if specified in userOptions, will be overwritten)
isAdmin: false,
dateCreated: new Date(),
// Specified values (including the username, which we kept as required)
...userOptions
};
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too