unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.89k stars 496 forks source link

feat(modules): infer modules types in config #2686

Closed Barbapapazes closed 1 month ago

Barbapapazes commented 1 month ago

πŸ”— Linked issue

fix #2685

❓ Type of change

πŸ“š Description

Hello πŸ‘‹,

This feature uses typegen to add modules types within the NitroConfig interface to allow autocompletion in the nitro.config.ts file.

With the following module:

interface ModuleOptions {
  bar: string;
  foo: {
    baz: string;
  };
}

export default defineNitroModule<ModuleOptions>({
  name: "playground",
  configKey: "playground",
  setup: (nitro, moduleOptions) => {},
});

TypeScript will help you with autocompletion when you are configuring the module in the nitro.config.ts file:

import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({
  playground: {
    bar: "bar",
    foo: "baz",
  },

  compatibilityDate: "2024-06-12",
});

Of course, the foo property should be an object, and TypeScript will raise an error.

Screenshot 2024-08-23 at 01 15 43

πŸ“ Checklist

pi0 commented 1 month ago

Hi, thanks for the PR i appreciate it but at the current state, i prefer to not depend more on auto generated types. it can both make conflict with higher level framework types and user experience.

Barbapapazes commented 1 month ago

Have you any plans to improve the DX of modules? Is it possible to help?

pi0 commented 1 month ago

Not until we officially release Nitro Kit and I think i am more likely to recommend direct module imports (like vite plugins that accept options inline) and not nuxt style -- this immediately supports type hints in IDE. but have to think more about it.