aurelia / framework

The Aurelia 1 framework entry point, bringing together all the required sub-modules of Aurelia.
MIT License
11.76k stars 625 forks source link

Allow better typing for module configuration #940

Open rmja opened 5 years ago

rmja commented 5 years ago

I'm submitting a feature request

Please tell us about your environment:

Current behavior: During intial configuration when modules are included, the module configuration argument is of type any:

aurelia.use.plugin("my-module", options?: any)

It is currently not possible for module authors to improve typings for their module, as aurelia.use is of type FrameworkConfiguration, and it is not possible to extend classes with typings in typescript.

Expected/desired behavior:

It would be really neat if the FramworkConfiguration implemented an interface, say IFramworkConfiguration for the builder api instead of the FrameworkConfiguration itself. The interface should of cause contain all all the properties/methods of FramworkConfiguration, and the return type for the methods should be changed to IFrameworkConfiguration.

This change would enable module authors to include typing aided configuration. Say for example:

import { IFrameworkConfiguration } from "aurelia-framework";
import { CacheOptions } from "./cache-options";

declare module "aurelia-framework" {
    interface IFrameworkConfiguration {
        plugin(plugin: "aurelia-workbox-cachecontrol", configureAction?: (options: CacheOptions) => unknown): FrameworkConfiguration;
    }
}

To make this work with PLATFORM.moduleName() in webpack environments, its typing definitation should be changed from:

moduleName(moduleName: string, options?: ModuleNameOptions): string;
moduleName(moduleName: string, chunk?: string): string;

to:

moduleName<T extends string>(moduleName: T, options?: ModuleNameOptions): T;
moduleName<T extends string>(moduleName: T, chunk?: string): T;

to specifically tell typescript that the same module name is returned.

image

EisenbergEffect commented 5 years ago

I don't think that we'l be able to address this in the current version of Aurelia. However, we've gathered a ton of feedback over the years on our plugin system in general and have made a bunch of improvements for our vNext. I'm going to close this out since we've already addressed this particular concern there.

EisenbergEffect commented 5 years ago

Actually, if all it really takes is the above modification of the types, I think we could do that in vCurrent possibly. I'm a bit nervous that this could break some people. We don't want to do that. If you want to submit a PR for this, we could discuss the implications further there.

bigopon commented 5 years ago

Yes, its doable, and a quality of life improvement that can help many typescript users. Can we reopen this? @rmja maybe you already have in mind ideas how to go forward, pelase help PR for this

EisenbergEffect commented 5 years ago

Re-opened 😄