unpreset / unocss-preset-theme

The dynamic theme presets for UnoCSS.
https://unocss-preset-theme.todev.cc
119 stars 11 forks source link

Typescript error in config #62

Closed Ladvace closed 10 months ago

Ladvace commented 10 months ago

I have the following config but I get this error:

// uno.config.ts
import { defineConfig } from "unocss";
import presetUno from "@unocss/preset-uno";
import type { Theme } from "unocss/preset-uno";
import presetTheme from "unocss-preset-theme";

export default defineConfig({
  // default theme
  theme: {},
  presets: [
    presetUno(),
    // custom themes
    presetTheme<Theme>({
      theme: {
        dark: {},
      },
    }),
  ],
});

Type 'Preset<Theme>' is not assignable to type 'Preset<any> | Preset<any>[]'. Type 'Preset<Theme>' is not assignable to type 'Preset<any>'. Types of property 'rules' are incompatible. Type 'Rule<Theme>[] | undefined' is not assignable to type 'Rule<any>[] | undefined'. Type 'Rule<Theme>[]' is not assignable to type 'Rule<any>[]'. Type 'Rule<Theme>' is not assignable to type 'Rule<any>'. Type '[RegExp, DynamicMatcher<Theme>]' is not assignable to type 'Rule<any>'. Type '[RegExp, DynamicMatcher<Theme>]' is not assignable to type '[string, CSSObject | CSSEntries] | [RegExp, DynamicMatcher<any>]'. Type '[RegExp, DynamicMatcher<Theme>]' is not assignable to type '[string, CSSObject | CSSEntries]'. Type at position 0 in source is not compatible with type at position 0 in target. Type 'RegExp' is not assignable to type 'string'.ts(2322)

Dunqing commented 10 months ago

I think it's okay now, upgrade to the latest version.

Mintnoii commented 9 months ago

Version 0.9.3 still has this problem,my unocss version is 0.56.0 @Dunqing

Dunqing commented 9 months ago

Version 0.9.3 still has this problem,my unocss version is 0.56.0 @Dunqing

Thank you for the feedback.

This is a problem caused by the inconsistency between your unocss version and the unocss version of unocss-preset-theme. Normally it will only cause a type error, so you can ignore it and wait until this preset is synchronized with the latest version of unocss to fix it.

Now you can update to 0.10.0 and the problem will be gone!

wtto00 commented 7 months ago

@Dunqing Hello, this issue has resurfaced in version 0.11.0, and my unocss version is 0.57.3.

Can the following types of changes be made?

interface PresetThemeOptions<Theme extends Record<string, any>> {
  theme: Record<string, Theme>
}
// changed to blow
interface Colors {
    [key: string]: Colors & {
        DEFAULT?: string;
    } | string;
}
interface PresetThemeOptions<Theme extends Record<string, any>> {
  theme: Record<string, { colors: Colors }>
  // or Record<string, Colors>
}

Colors type from https://github.com/unocss/unocss/blob/af91b66e141794489e49bffd521d989dd8b76888/packages/preset-mini/src/_theme/types.ts#L11-L13

Remove all other configuration options and keep only colors. Users can use dark:xxx in their business code for condition checking. This plugin should only handle color-related logic.