I am looking for a solution to prevent my function from working with Moment objects when storing values in local storage. Currently, the function dynamically stringifies and stores values, but I want to exclude Moment objects from being processed. Here is an example of the syntax:
public static set<TValue>(
key: LocalStorageKeyEnum,
value: TValue,
...keySuffixes: string[]
) {
localStorage.setItem(
LocalStorage.buildKey(key, keySuffixes),
JSON.stringify(value)
)
}
If the second argument passed to the function is a Moment object, it works fine, but I want to restrict this behavior by excluding Moment objects from <TValue>
. Is there a way to achieve this using Typescript without resorting to run-time checks? One approach could be to create two types, one that includes all values and another that excludes Moment objects, like this:
type Variants = {
value: TValue,
date: moment.Moment
}
type ExcludeDate = Exclude {typeof Variants, "date"}
However, I am curious if there are alternative methods to achieve this goal. Any suggestions or advice would be greatly appreciated! As I am new to Typescript, I apologize if my question is unclear.