From d02125b156bf4512233beaa3f70bbca79089441f Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Mon, 9 Jan 2023 16:56:32 -0700 Subject: [PATCH] required options can't have defaults in optionize defaults, https://github.com/phetsims/chipper/issues/1360 --- js/optionize.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/js/optionize.ts b/js/optionize.ts index 80e9046..075945d 100644 --- a/js/optionize.ts +++ b/js/optionize.ts @@ -27,6 +27,10 @@ type OptionalKeys = { // Gets the parts of an object that are optional type Options = Pick>; +export type RequiredKeys = { + [K in keyof T]-?: object extends Pick ? never : K; +}[keyof T]; + type ObjectWithNoKeys = Record; export type EmptySelfOptions = { @@ -36,11 +40,14 @@ export type EmptySelfOptions = { type EmptySelfOptionsKeys = keyof EmptySelfOptions; // This is the type for the `defaults` argument to optionize -type OptionizeDefaults = +type OptionizeDefaults = // Everything optional from SelfOptions must have a default specified Omit>, EmptySelfOptionsKeys> & // eslint-disable-line @typescript-eslint/ban-types + // Anything required in the ProvidedOptions should not show up in + { [k in RequiredKeys]?: never; } & + // Any or none of Parent options can be provided Partial; @@ -55,7 +62,7 @@ export default function optionize>(): ( - defaults: OptionizeDefaults, + defaults: OptionizeDefaults, providedOptions?: ProvidedOptions ) => OptionizeDefaults & ProvidedOptions & Required> { return merge4;