I am writing a type for nested routes array. The array can contain either a prefixed routes object or a simple route object.
The conditions are:
- If it is a simple route object, then it must contain only the properties that are mentioned in
IApiRoute
. - If it is a prefixed route object that it must contain two keys only
2 a. First key isprefix
as mentioned inIPrefixApiRoute
.
2 b. Second key isroutes
array, it can contain an array of objects mentioned in point 2 and point 1.
I have created the typings, but there is a problem, it is not giving a compile time error if I create an object like this
const array: Array<IRawApiRoute> = [
{
prefix: "auth",
routes: [],
method: "GET", // This key shouldn't be allowed here, as this object can contain only the keys from IPrefixApiRoute
},
];
The typings that I have prepared are:
type Method = "GET" | "DELETE" | "HEAD" | "OPTIONS" | "POST" | "PUT" | "PATCH";
interface IApiRoute {
route: string;
method: Method;
routeParams: string[];
queryParams: string[];
name: string;
}
interface IPrefixApiRoute {
prefix: string;
routes: Array<IRawApiRoute>;
}
type IRawApiRoute = IApiRoute | IPrefixApiRoute;
I can't seem to figure out the problem. Any help would be appreciated. Thanks