Skip to content

Commit

Permalink
🐛 fix: 修正类型定义不准确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jan 10, 2023
1 parent 0a953c9 commit d318382
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/functions/createStyish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import { createStyles, StyleOrGetStyleFn } from './createStyles';
/**
* 业务应用中创建复合通用样式的进阶
*/
export function createStylish<Props, Obj>(cssStyleOrGetCssStyleFn: StyleOrGetStyleFn<Props, Obj>) {
export function createStylish<Props, Styles>(
cssStyleOrGetCssStyleFn: StyleOrGetStyleFn<Props, Styles>,
) {
// FIXME :类型定义
// @ts-ignore
const useStyles = createStyles(cssStyleOrGetCssStyleFn);

return (props?: Props): ReturnStyleToUse<Obj> => {
return (props?: Props): ReturnStyleToUse<Styles> => {
const { styles } = useStyles(props);

// FIXME :类型定义
// @ts-ignore
return styles;
};
}
9 changes: 5 additions & 4 deletions src/functions/createStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ export type StyleOrGetStyleFn<Input, P> = StyleDefinition<Input> | GetStyleFn<In
// 获取样式
export type GetStyleFn<Input, P> = (theme: CreateStylesTheme, props?: P) => StyleDefinition<Input>;

export type UseStyleS<Input, P> = (props?: P) => ReturnStyles<Input>;
export type UseStyleHook<Input, P> = (props?: P) => ReturnStyles<Input>;

interface CreateStylesFn {
<Props, Input>(style: StyleDefinition<Input>): UseStyleS<Input, Props>;
<Props, Input>(getCssStyleFn: GetStyleFn<Input, Props>): UseStyleS<Input, Props>;
<Props, Input>(style: StyleDefinition<Input>): UseStyleHook<Input, Props>;
<Props, Input>(getCssStyleFn: GetStyleFn<Input, Props>): UseStyleHook<Input, Props>;
}

/**
* 业务应用中创建样式基础写法
*/
export const createStyles: CreateStylesFn =
<Input, P>(styleOrGetStyleFn: StyleOrGetStyleFn<Input, P>) =>
<P, Input>(styleOrGetStyleFn: StyleOrGetStyleFn<Input, P>) =>
(props?: P): ReturnStyles<Input> => {
const theme = useTheme();

Expand Down
10 changes: 8 additions & 2 deletions src/types/genericUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import { CSSObject } from '@emotion/css';
*/
export type StyleDefinition<StyleObj> = StyleObj extends string
? string
: Record<keyof StyleObj, CSSObject | string>;
: {
[Key in keyof StyleObj]: CSSObject | string;
};

/**
* 根据用户返回的样式对象,返回一个可以给用户使用的
* 譬如用户输入为 { a: css`color: red;`, b: { color: 'red' }
* 输出的类型泛型为 { a:string; b:string }
*/
export type ReturnStyleToUse<T> = T extends Record<infer R, any> ? Record<R, string> : string;
export type ReturnStyleToUse<T> = T extends string
? string
: {
[Key in keyof T]: string;
};

0 comments on commit d318382

Please sign in to comment.