Skip to content

Commit

Permalink
required options can't have defaults in optionize defaults, phetsims/…
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Jan 9, 2023
1 parent befa2f7 commit d02125b
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions js/optionize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type OptionalKeys<T> = {
// Gets the parts of an object that are optional
type Options<T> = Pick<T, OptionalKeys<T>>;

export type RequiredKeys<T> = {
[K in keyof T]-?: object extends Pick<T, K> ? never : K;
}[keyof T];

type ObjectWithNoKeys = Record<string | number, never>;

export type EmptySelfOptions = {
Expand All @@ -36,11 +40,14 @@ export type EmptySelfOptions = {
type EmptySelfOptionsKeys = keyof EmptySelfOptions;

// This is the type for the `defaults` argument to optionize
type OptionizeDefaults<SelfOptions = EmptySelfOptions, ParentOptions = EmptySelfOptions> =
type OptionizeDefaults<SelfOptions = EmptySelfOptions, ParentOptions = EmptySelfOptions, ProvidedOptions = EmptySelfOptions> =

// Everything optional from SelfOptions must have a default specified
Omit<Required<Options<SelfOptions>>, EmptySelfOptionsKeys> & // eslint-disable-line @typescript-eslint/ban-types

// Anything required in the ProvidedOptions should not show up in
{ [k in RequiredKeys<ProvidedOptions>]?: never; } &

// Any or none of Parent options can be provided
Partial<ParentOptions>;

Expand All @@ -55,7 +62,7 @@ export default function optionize<ProvidedOptions,
SelfOptions = ProvidedOptions,
ParentOptions = Record<never, never>>():
<KeysUsedInSubclassConstructor extends keyof ( ParentOptions )>(
defaults: OptionizeDefaults<SelfOptions, ParentOptions>,
defaults: OptionizeDefaults<SelfOptions, ParentOptions, ProvidedOptions>,
providedOptions?: ProvidedOptions
) => OptionizeDefaults<SelfOptions, ParentOptions> & ProvidedOptions & Required<Pick<ParentOptions, KeysUsedInSubclassConstructor>> {
return merge4;
Expand Down

0 comments on commit d02125b

Please sign in to comment.