-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: None
-
2 - S (<= 1 week)
-
3593
-
Not Needed
Goals
In it's current form the App constructor takes generic type arguments enabling developers to "lock down" the type of the "functions factory", user custom data and profile data. As this pattern grows however, it's annoying for developers to have to provide these as positional type arguments to the App class. Instead we should support "named" generic type aka. a single AppTypes generic type which enumerates the individual types used throughout the app.
Below is a proff of concept:
Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
// The following are types that are internal to the SDK type BaseFunctionsFactory = { callFunction(name: string, args: unknown[]): Promise<unknown>; }; type DefaultFunctionsFactory = BaseFunctionsFactory & { [name: string]: (...args: unknown[]) => Promise<unknown>; }; type BaseAppTypes = { FunctionsFactory: unknown, CustomUserDataType: unknown, ProfileDataType: unknown }; type AppTypes<T extends Partial<BaseAppTypes>> = T & BaseAppTypes; declare class User<T extends BaseAppTypes> { get functions(): T["FunctionsFactory"]; get data(): T["CustomUserDataType"]; } class App<T extends BaseAppTypes> { currentUser: User<T> | null = null; } // Below are the types as used from an app // The user can declare their own app's types, wrapping them in AppTypes to allow defaults to be applied for (sub)types that is not declared. type MyAppTypes = AppTypes<{ FunctionsFactory: { echo: () => Promise<void> }, }>; // Using the types const app = new App<MyAppTypes>(); if (app.currentUser) { app.currentUser.functions.echo(); }