I recently stumbled upon this code snippet in a coding playground where TypeScript is used:
export interface Page {
heading: string;
component: string;
path: string;
}
export type RouteOnly = Pick<Page, 'heading' | 'path'>;
export const routes: (Page | RouteOnly)[] = [
{
heading: 'Home',
path: '/home',
component: 'A',
},
{
heading: 'OSS',
path: '/oss',
component: 'B',
},
{
heading: 'CV',
path: '/cv'
}
];
export function isPage(pageOrRoute: Page | RouteOnly): pageOrRoute is Page {
return !!(pageOrRoute as Page).component;
}
const pages: Page[] = routes.filter((r) => isPage(r));
After examining the code, it appears that the pages
array should only contain pages, but TypeScript is throwing an error:
Property 'component' is missing in type 'Pick' but required in type 'Page'