Skip to content

Commit

Permalink
undo all theme types, and type it as theme(path: string): any
Browse files Browse the repository at this point in the history
Since currently we don't want to investigate time to make the code
completion *perfect* (because it might be even impossible to do it
properly due to resolving of overrides, extend and deeply nested presets)

For now we will provide a way simpler type, which is better than
incorrect types. So far we only had types for the default config theme
*only*.
  • Loading branch information
RobinMalfait committed Mar 31, 2022
1 parent 4147433 commit fa6c107
Showing 1 changed file with 1 addition and 46 deletions.
47 changes: 1 addition & 46 deletions types/config.d.ts
Expand Up @@ -13,53 +13,9 @@ interface RecursiveKeyValuePair<K extends keyof any = string, V = string> {
}
type ResolvableTo<T> = T | ((utils: PluginUtils) => T)

type DotNotation<T, K extends keyof T = keyof T> = K extends string
? T[K] extends Record<string, any>
? T[K] extends any[]
? K | `${K}.${DotNotation<T[K], Exclude<keyof T[K], keyof any[]>>}`
: K | `${K}.${DotNotation<T[K], keyof T[K]>}`
: K
: never
type Path<T> = DotNotation<T> | keyof T
type PathValue<T, P extends Path<T>> = P extends `${infer K}.${infer Rest}`
? K extends keyof T
? Rest extends Path<T[K]>
? PathValue<T[K], Rest>
: never
: never
: P extends keyof T
? T[P]
: never

// Removes arbitrary values like: { [key: string]: any }
type WithoutStringKey<T> = { [K in keyof T as string extends K ? never : K]: T[K] }

type Unpack<T> = T extends ResolvableTo<infer R>
? { [K in keyof R]: Unpack<R[K]> }
: T extends object
? { [K in keyof T]: Unpack<T[K]> }
: 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<WithoutStringKey<Unpack<Config['theme']>>, 'extend'>

// This will add additional information purely for code completion. E.g.:
type AugmentedTheme = Expand<Omit<UnpackedTheme, 'colors'> & { colors: DefaultColors }>

interface PluginUtils {
colors: DefaultColors

// Dynamic based on (default) theme config
theme<P extends Path<AugmentedTheme>, TDefaultValue>(
path: P,
defaultValue?: TDefaultValue
): PathValue<AugmentedTheme, P>
// Path is just a string, useful for third party plugins where we don't
// know the resulting type without generics.
theme<TDefaultValue = any>(path: string, defaultValue?: TDefaultValue): TDefaultValue

theme(path: string, defaultValue?: unknown): any
breakpoints<I = Record<string, unknown>, 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
Expand Down Expand Up @@ -367,4 +323,3 @@ interface OptionalConfig {
}

export type Config = RequiredConfig & Partial<OptionalConfig>

0 comments on commit fa6c107

Please sign in to comment.