I am interested in achieving the following:
interface IJSON {
[key: string]: string | number | boolean | IJSON |
string[] | number[] | boolean[] | IJSON[];
}
function iAcceptOnlyJSON<T subsetof IJSON>(json: T): T {
return json;
}
let good = {
message: 'it works',
};
let bad = {
date: new Date(),
};
// Since good is a subset of IJSON, everything functions as expected!
iAcceptOnlyJSON(good);
// However, bad is not a subset of IJSON which should result in an error
iAcceptOnlyJSON(bad);
It's worth noting that there isn't a "subsetof" operator in Typescript. Is there a method to achieve this functionality in typescript?