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

New Type "RequiredByValueExcept<T, ValueType>", "PartialByValueExcept<T, ValueType>"

Open black7375 opened this issue 2 years ago • 0 comments

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

This is useful when defining parameter values ​​for functions.

Describe a solution including usage in code example

interface optionI {
  inputPath: string;
  outputPath: string;
  format: string;
}

const defaultOption: RequiredByValueExcept<optionI, 'inputPath' | 'outputPath'> = {
  format: 'txt'
};

function foo(text: string, option: PartialByValueExcept<optionI, 'inputPath' | 'format'>) {
  // ...
}

Who does this impact? Who is this for?

I think this would be useful for defining default values ​​and parameters.

Describe alternatives you've considered (optional)

Additional context (optional)

The naming convention has been unified. https://stackoverflow.com/questions/52703321/make-some-properties-optional-in-a-typescript-type

type RequiredByValueExcept<T, TOptional extends keyof T> = Pick<T, Diff<keyof T, TOptional>> & Partial<T>;
type PartialByValueExcept<T, TRequired extends keyof T> = Partial<T> & Pick<T, TRequired>;

black7375 avatar Oct 15 '21 01:10 black7375