#Type that is either has properties or empty object

2 messages · Page 1 of 1 (latest)

craggy pond
#

I have this CSF type that looks like:

export type CSF = {
    id: string;
    name: string;
    chemical: string;
    EPANumber: string;
    ingID: number;
    ingredient: string;
    types: string[];
    dateModified: string;
};

And now I have this state variable: const [optionsCSF, setOptionsCSF] = useState<CSF | {}>({});
And finally this line of code: const newCSFList = CSFList.filter((c: CSF) => optionsCSF && c.name !== optionsCSF?.name);
I am getting this error: TS2339: Property  name  does not exist on type  {} | CSF  Property  name  does not exist on type  {} 

river path
#

More of an opinion or a "what I do"... I usually do the "no value" as null rather than an empty object. This allows me to do if (optionsCSF) { ... }.

If you do want to stick with an empty object, you can maybe define it as Record<string, never> to signal that it must be CSF or nothing