Theme<>
type Theme<T extends VarGroup<unknown, symbol>>
Theme
は、指定されたVarGroup
内の変数の集合をテーマとするスタイルを表す型です。stylex.createTheme
を呼び出した結果です。
import type {VarGroup} from '@stylexjs/stylex';
import * as stylex from '@stylexjs/stylex';
import {vars} from './vars.stylex';
export const theme: Theme<typeof vars> = stylex.createTheme(vars, {
color: 'red', // OK
backgroundColor: 'blue', // OK
});
Theme
の最も一般的なユースケースは、特定の変数のセットのテーマを受け入れることです。
ほとんどの場合必要ありませんが、Theme
はオプションの2番目の型の引数も受け入れます。
高度なユースケース:`Theme`の独自の型ID
同じVarGroup
の2つのテーマは、デフォルトで同じ型になります。ほとんどの場合、これは望ましい動作です。しかし、場合によっては、特定のコンポーネントに渡すことができるテーマを制約するために、各テーマが独自の型IDを持つようにしたい場合があります。
import * as stylex from '@stylexjs/stylex';
import type {Theme} from '@stylexjs/stylex';
import {vars} from './vars.stylex';
declare const Tag: unique symbol;
export const theme1: Theme<typeof vars, Tag> = stylex.createTheme(vars, {
color: 'red', // OK
backgroundColor: 'blue', // OK
});
これで、theme1
には独自の型IDがあり、vars
の別のTheme
はtypeof theme1
を満たさなくなります。
この高度なユースケースはあまり必要ありませんが、必要な場合は使用できます。