Created
June 11, 2023 06:51
-
-
Save joanna-liana/4d3bd30f16aa5b163a0f9c9bf6ad7bdc to your computer and use it in GitHub Desktop.
Bullet-proof defaults for object parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const withDefaults = ( | |
{ | |
a = 'a default', | |
b | |
}: { | |
a?: string; | |
b?: number; | |
} | |
) => console.log(a, b); | |
// "aaa", 1 | |
withDefaults({ a: "aaa", b: 1 }); | |
// "a default", 2 | |
withDefaults({ b: 2 }); | |
// "a default", undefined | |
withDefaults({}); | |
// "a default", undefined | |
withDefaults(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment