| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.resolveDefaultProps = resolveDefaultProps;
|
|---|
| 7 | function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|---|
| 8 | function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|---|
| 9 | function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|---|
| 10 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 11 | function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|---|
| 12 | /**
|
|---|
| 13 | * This function mimics the behavior of the `defaultProps` static property in React.
|
|---|
| 14 | * Functional components do not have a defaultProps property, so this function is useful to resolve default props.
|
|---|
| 15 | *
|
|---|
| 16 | * The common recommendation is to use ES6 destructuring with default values in the function signature,
|
|---|
| 17 | * but you need to be careful there and make sure you destructure all the individual properties
|
|---|
| 18 | * and not the whole object. See the test file for example.
|
|---|
| 19 | *
|
|---|
| 20 | * And because destructuring all properties one by one is a faff, and it's easy to miss one property,
|
|---|
| 21 | * this function exists.
|
|---|
| 22 | *
|
|---|
| 23 | * @param realProps - the props object passed to the component by the user
|
|---|
| 24 | * @param defaultProps - the default props object defined in the component by Recharts
|
|---|
| 25 | * @returns - the props object with all the default props resolved. All `undefined` values are replaced with the default value.
|
|---|
| 26 | */
|
|---|
| 27 | function resolveDefaultProps(realProps, defaultProps) {
|
|---|
| 28 | /*
|
|---|
| 29 | * To avoid mutating the original `realProps` object passed to the function, create a shallow copy of it.
|
|---|
| 30 | * `resolvedProps` will be modified directly with the defaults.
|
|---|
| 31 | */
|
|---|
| 32 | var resolvedProps = _objectSpread({}, realProps);
|
|---|
| 33 | /*
|
|---|
| 34 | * Since the function guarantees `D extends Partial<T>`, this assignment is safe.
|
|---|
| 35 | * It allows TypeScript to work with the well-defined `Partial<T>` type inside the loop,
|
|---|
| 36 | * making subsequent type inference (especially for `dp[key]`) much more straightforward for the compiler.
|
|---|
| 37 | * This is a key step to improve type safety *without* value assertions later.
|
|---|
| 38 | */
|
|---|
| 39 | var dp = defaultProps;
|
|---|
| 40 | /*
|
|---|
| 41 | * `Object.keys` doesn't preserve strong key types - it always returns Array<string>.
|
|---|
| 42 | * However, due to the `D extends Partial<T>` constraint,
|
|---|
| 43 | * we know these keys *must* also be valid keys of `T`.
|
|---|
| 44 | * This assertion informs TypeScript of this relationship, avoiding type errors when using `key` to index `acc` (type T).
|
|---|
| 45 | *
|
|---|
| 46 | * Type assertions are not sound but in this case it's necessary
|
|---|
| 47 | * as `Object.keys` does not do what we want it to do.
|
|---|
| 48 | */
|
|---|
| 49 | var keys = Object.keys(defaultProps);
|
|---|
| 50 | var withDefaults = keys.reduce((acc, key) => {
|
|---|
| 51 | if (acc[key] === undefined && dp[key] !== undefined) {
|
|---|
| 52 | acc[key] = dp[key];
|
|---|
| 53 | }
|
|---|
| 54 | return acc;
|
|---|
| 55 | }, resolvedProps);
|
|---|
| 56 | /*
|
|---|
| 57 | * And again type assertions are not safe but here we have done the runtime work
|
|---|
| 58 | * so let's bypass the lack of static type safety and tell the compiler what happened.
|
|---|
| 59 | */
|
|---|
| 60 | return withDefaults;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Helper type to extract the keys of T that are required.
|
|---|
| 65 | * It iterates through each key K in T. If Pick<T, K> cannot be assigned an empty object {},
|
|---|
| 66 | * it means K is required, so we keep K; otherwise, we discard it (never).
|
|---|
| 67 | * [keyof T] at the end creates a union of the kept keys.
|
|---|
| 68 | */
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Helper type to extract the keys of T that are optional.
|
|---|
| 72 | * It iterates through each key K in T. If Pick<T, K> can be assigned an empty object {},
|
|---|
| 73 | * it means K is optional (or potentially missing), so we keep K; otherwise, we discard it (never).
|
|---|
| 74 | * [keyof T] at the end creates a union of the kept keys.
|
|---|
| 75 | */
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Helper type to ensure keys of D exist in T.
|
|---|
| 79 | * For each key K in D, if K is also a key of T, keep the type D[K].
|
|---|
| 80 | * If K is NOT a key of T, map it to type `never`.
|
|---|
| 81 | * An object cannot have a property of type `never`, effectively disallowing extra keys.
|
|---|
| 82 | */
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * This type will take a source type `Props` and a default type `Defaults` and will return a new type
|
|---|
| 86 | * where all properties that are optional in `Props` but required in `Defaults` are made required in the result.
|
|---|
| 87 | * Properties that are required in `Props` and optional in `Defaults` will remain required.
|
|---|
| 88 | * Properties that are optional in both `Props` and `Defaults` will remain optional.
|
|---|
| 89 | *
|
|---|
| 90 | * This is useful for creating a type that represents the resolved props of a component with default props.
|
|---|
| 91 | */ |
|---|