It it possible to leverage type-checking to enforce a page endpoint's body returns required props?

Viewed 29

Consider the following page and page endpoint:

routes/foo.svelte

<script lang="ts">
  export let bar: string;
</script>

...

routes/foo.ts

export type Props = { bar: string };

export const get: RequestHandler<{}, Props> = () => {
   return {};
}

Even though bar is a required prop on the component, the endpoint can return without body being defined. Consequently, required props aren't supplied to the component. Is it possible to have typescript enforce that a body be returned with the expected type?

Furthermore, is it possible to define a component's props such that they formally conform to the Props interface?

0 Answers
Related