eidellev / inertiajs-adonisjs

278 stars 17 forks source link

Suggested way to infer Params returned #109

Open codytooker opened 1 year ago

codytooker commented 1 year ago

Is it possible to infer the types returned form inertia.render(). This would be a huge value proposition I think for inertia with adonis.

As a quick example say I have a controller like so

import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

export default class FooController {
  public async index({ inertia }: HttpContextContract) {

    return inertia.render('Foo', {
      title: "This is the title",
    })
  }
}

With React and Typescript I currently do something like this

interface FooPageProps {
   title: string
}

const FooPage = ({ title }: FooPageProps) => {
    return <div>{title}</div>
}

The problem here is that we aren't really getting good types because we are basically just telling the component what it has not what it will actually receive. It would be extremely valuable to do something like the following

const FooPage = ({ title }: InertiaResponse<FooController['show']>) => {
    return <div>{title}</div>
}

I am not a Typescript expert but I really wonder if this could be possible using some combination of ReturnType and infer

eidellev commented 1 year ago

Hey @codytooker ! I really like the idea and definitely see the value here. It will take some next level TS wizardry but I think it can be done.

codytooker commented 1 year ago

There is also the problem of Shared Data which may or not be available on certain routes, so this may not ever really be totally possible but I still think it would help in the long run. I'll investigate as well during my project and if I find anything I'll post back here.