elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
9.23k stars 198 forks source link

Type inference not working for derive/resolve coming from plugin #580

Closed JohnRoseDev closed 4 months ago

JohnRoseDev commented 4 months ago

What version of Elysia.JS is running?

1.0.9

What platform is your computer?

Darwin 22.5.0 x86_64 i386

What steps can reproduce the bug?

The docs introduce a service locator pattern in order to correctly use decorated values in other instances (https://elysiajs.com/essential/plugin.html#service-locator).

This works:

import { Elysia } from 'elysia'

const setup = new Elysia({ name: 'setup' }
  .decorate(() => {
    return { user: 'user' };
  })

const main = new Elysia()
  .use(setup)
  .get('/', ({ user }) => user);

But using derive or resolve instead of decorate will not work:

import { Elysia } from 'elysia'

const setup = new Elysia({ name: 'setup' })
  .resolve(() => { // same happens with derive
    return { user: 'user' };
  })

const main = new Elysia()
  .use(setup)
  .get('/', ({ user }) => user); // TS2339: Property  user  does not exist on type ...

Is it not possible to encapsulate resolve/derive logic into plugins while maintaining type inference? Is this a bug or limitation of Elysia?

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

cybercoder-naj commented 4 months ago

Refer #513

JohnRoseDev commented 4 months ago

Refer #513

Thanks. Setting the hooks as global fixes the TS issues.