Let's say I want to create an interface that looks like this:
interface DataStatus<T> {
isSuccess: boolean;
isError: boolean;
data: T | undefined;
}
And later on I'm going to be using this like:
interface Foo {
id: string;
name: string;
}
function fetchData() : DataStatus<Foo> {
//implementation
}
const ds = fetchData();
if (ds.isSuccess) {
console.log(ds.data.name); //TS Warning - ds.data might be undefined
}
What I'd like to do add some conditions to the DataStatus interface with these rules:
isSuccessandisErrormust be oppositesdatawill have valueTifisSuccessis true, and beundefinedifisSuccessis false
Is this kind of thing possible with typescript?