| 48 | } |
| 49 | |
| 50 | function defaultOrInitializer<S>( |
| 51 | defaultValueOrInitializeFn: S | ((value?: unknown, rawStorageValue?: unknown) => S), |
| 52 | value?: unknown, |
| 53 | rawValue?: unknown |
| 54 | ): S { |
| 55 | if (typeof defaultValueOrInitializeFn === 'function') { |
| 56 | // https://github.com/microsoft/TypeScript/issues/37663#issuecomment-759728342 |
| 57 | // @ts-expect-error TS(2349): This expression is not callable. |
| 58 | return defaultValueOrInitializeFn(value, rawValue); |
| 59 | } |
| 60 | return value === undefined ? defaultValueOrInitializeFn : (value as S); |
| 61 | } |
| 62 | |
| 63 | // Initialize state with default value or value from localStorage. |
| 64 | // If window is not defined uses the default value and **does not** throw an error |