utility-types icon indicating copy to clipboard operation
utility-types copied to clipboard

Add new type: Flip<T>, FlipOptional<T>, Invert<T> or InvertOptional<T> (I'm not sure of the name)

Open branchard opened this issue 5 years ago • 0 comments
trafficstars

Is your feature request related to a real problem or use-case?

This type can be usefull to reverse required and optional properties of an object. Related question: https://stackoverflow.com/questions/57593022/reverse-required-and-optional-properties.

Describe a solution including usage in code example

Solution:

import { RequiredKeys, OptionalKeys } from 'utility-types';
export declare type Flip<T> = Partial<Pick<T, RequiredKeys<T>>> & Required<Pick<T, OptionalKeys<T>>>;

Usage:

import { Flip } from 'utility-types';

type MyObject = {
  a: number
  b?: number
  c?: number
};

type MyFlippedObject = Flip<MyObject>;
// Should be
// type MyObject = {
//  a?: number
//  b: number
//  c: number
// };

Who does this impact? Who is this for?

TypeScript users.

branchard avatar Dec 23 '19 02:12 branchard