From 8d611e0830e27de14dc1a44cab500f393f8bd21c Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 23 Mar 2022 18:04:33 +0100 Subject: [PATCH] WIP: `theme()` utility function code completion This will give you code completion in the `theme()` function. The reason it is still a WIP is that this only works with the default config right now and not 100% sure if it is possible to define generics in JSDoc. The idea would be to: - Provide types from the default config - Provide types from the custom config (e.g.: 3rd party plugin) - Override default config types with the overrides of the user's config Right now this only provides types for the defaultConfig which might result in dropping all of this in favor of a much simpler: ```ts theme(path: string, defaultValue: D) => D ``` But this sadly doesn't give you "nice" auto completion. However, the default might be good enough if we don't error on for example `theme('customPlugin')` which is currently not the case. --- types/config.d.ts | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/types/config.d.ts b/types/config.d.ts index ef2a8f4bc35b..9460884422e6 100644 --- a/types/config.d.ts +++ b/types/config.d.ts @@ -13,9 +13,53 @@ interface RecursiveKeyValuePair { } type ResolvableTo = T | ((utils: PluginUtils) => T) +type DotNotation = K extends string + ? T[K] extends Record + ? T[K] extends any[] + ? K | `${K}.${DotNotation>}` + : K | `${K}.${DotNotation}` + : K + : never +type Path = DotNotation | keyof T +type PathValue> = P extends `${infer K}.${infer Rest}` + ? K extends keyof T + ? Rest extends Path + ? PathValue + : never + : never + : P extends keyof T + ? T[P] + : never + +// Removes arbitrary values like: { [key: string]: any } +type WithoutStringKey = { [K in keyof T as string extends K ? never : K]: T[K] } + +type Unpack = T extends ResolvableTo + ? { [K in keyof R]: Unpack } + : T extends object + ? { [K in keyof T]: Unpack } + : T + +// This will remove all the callbacks and simplify it to the actual object +// it uses or the object it returns. It will also remove the `extend` +// section because at runtime that's gone anyway. +type UnpackedTheme = Omit>, 'extend'> + +// This will add additional information purely for code completion. E.g.: +type AugmentedTheme = Expand & { colors: DefaultColors }> + interface PluginUtils { colors: DefaultColors - theme(path: string, defaultValue: unknown): keyof ThemeConfig + + // Dynamic based on (default) theme config + theme

, TDefaultValue>( + path: P, + defaultValue?: TDefaultValue + ): PathValue + // Path is just a string, useful for third party plugins where we don't + // know the resulting type without generics. + theme(path: string, defaultValue?: TDefaultValue): TDefaultValue + breakpoints, O = I>(arg: I): O rgb(arg: string): (arg: Partial<{ opacityVariable: string; opacityValue: number }>) => string hsl(arg: string): (arg: Partial<{ opacityVariable: string; opacityValue: number }>) => string