@@ -34,14 +34,17 @@ interface StyledComponent<T extends object> {
3434 < P > (
3535 styles : TemplateStringsArray ,
3636 ...expressions : (
37- | ExpressionType < BaseContext < P & ExtractPropTypes < PropsDefinition < T > > > >
38- | ExpressionType < BaseContext < P & ExtractPropTypes < PropsDefinition < T > > > > [ ]
37+ | ExpressionType < BaseContext < P & T > >
38+ | ExpressionType < BaseContext < P & T > > [ ]
3939 ) [ ]
40- ) : DefineSetupFnComponent < { as ?: string , props ?: P } & ExtractPropTypes < PropsDefinition < T > > & HTMLAttributes >
40+ ) : DefineSetupFnComponent < { as ?: string , props ?: P } & ExtractPropTypes < PropsDefinition < T & P > > & HTMLAttributes >
4141
4242 attrs : < A = object > (
43- attrs : A | ( ( props : ExtractPropTypes < PropsDefinition < T > > ) => A ) ,
43+ attrs : object | ( ( props : PropsDefinition < T > & A ) => object ) ,
4444 ) => StyledComponent < A & ExtractPropTypes < PropsDefinition < T > > >
45+
46+ // 支持泛型参数的类型定义
47+ < P extends object > ( props : PropsDefinition < P > ) : StyledComponent < P & T >
4548}
4649
4750function baseStyled < T extends object > ( target : string | InstanceType < any > , propsDefinition ?: PropsDefinition < T > ) : StyledComponent < T > {
@@ -50,17 +53,25 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
5053 }
5154 let defaultAttrs : unknown
5255 function styledComponent < P > (
53- styles : TemplateStringsArray ,
56+ stylesOrProps : TemplateStringsArray | PropsDefinition < P > ,
5457 ...expressions : (
5558 | ExpressionType < BaseContext < P & ExtractPropTypes < PropsDefinition < T > > > >
5659 | ExpressionType < BaseContext < P & ExtractPropTypes < PropsDefinition < T > > > > [ ]
5760 ) [ ]
5861 ) {
59- const cssStringsWithExpression = insertExpressions ( styles , expressions )
62+ // 处理泛型参数的情况,如 styled.div<Props>
63+ if ( ! Array . isArray ( stylesOrProps ) ) {
64+ return baseStyled ( target , { ...propsDefinition , ...stylesOrProps } as PropsDefinition < T & P > ) as StyledComponent < T & P >
65+ }
66+
67+ // 正常的样式模板字符串处理
68+ const cssStringsWithExpression = insertExpressions ( stylesOrProps as TemplateStringsArray , expressions )
6069 return createStyledComponent < P > ( cssStringsWithExpression )
6170 }
6271
63- styledComponent . attrs = function < A extends object > ( attrs : A ) {
72+ styledComponent . attrs = function < A = object > (
73+ attrs : object | ( ( props : ExtractPropTypes < PropsDefinition < T > > & A ) => object ) ,
74+ ) {
6475 defaultAttrs = attrs
6576 return styledComponent
6677 }
@@ -174,11 +185,14 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
174185 return styledComponent as StyledComponent < T >
175186}
176187
177- /** Append all the supported HTML elements to the styled properties */
178- const styled = baseStyled as typeof baseStyled & {
179- [ E in SupportedHTMLElements ] : ReturnType < typeof baseStyled >
188+ // 为styled添加attrs方法的类型定义
189+ type StyledInterface = typeof baseStyled & {
190+ [ E in SupportedHTMLElements ] : StyledComponent < object >
180191}
181192
193+ /** Append all the supported HTML elements to the styled properties */
194+ const styled = baseStyled as StyledInterface
195+
182196domElements . forEach ( ( domElement : SupportedHTMLElements ) => {
183197 styled [ domElement ] = baseStyled ( domElement )
184198} )
0 commit comments