[Typescript] Write clean Type 3 - make a wrapper to cleanup generic usages
Original code:
import { CSSProperties } from "react";
const useStyled = <TTheme = {}>(func: (theme: TTheme) => CSSProperties) => {
// Imagine that this function hooks into a global theme
// and returns the CSSProperties
return {} as CSSProperties;
};
interface MyTheme {
color: {
primary: string;
};
fontSize: {
small: string;
};
}
const buttonStyle = useStyled<MyTheme>((theme) => ({
color: theme.color.primary,
fontSize: theme.fontSize.small,
}));
const divStyle = useStyled<MyTheme>((theme) => ({
backgroundColor: theme.color.primary,
}));
Notice that everytime we call useStyled<MyTheme>
, we have to pass generic. We want a way that we don't need to pass generic.
Solution:
import { CSSProperties } from 'react';
const makeUseStyle = <TTheme = {}>() => {
return (func: (theme: TTheme) => CSSProperties) => {
return {} as CSSProperties;
};
};
const useStyled = makeUseStyle<MyTheme>();
interface MyTheme {
color: {
primary: string;
};
fontSize: {
small: string;
};
}
const buttonStyle = useStyled((theme) => ({
color: theme.color.primary,
fontSize: theme.fontSize.small,
}));
const divStyle = useStyled((theme) => ({
backgroundColor: theme.color.primary,
}));
Now when use call useStyle
, we don't need to pass generic slot everytime.
This solution allows you to pass the generic once in the code and all the rest of code will get infer of those generic type.
Another example:
import { Equal, Expect } from '../helpers/type-utils';
export const makeSelectors =
<TSource = 'TSource expects one arguement'>() =>
<TSelectors extends Record<string, (source: TSource) => any>>(
selectors: TSelectors
) => {
return selectors;
};
interface Source {
firstName: string;
middleName: string;
lastName: string;
}
const useSelector = makeSelectors<Source>();
const selectors = useSelector({
getFullName: (source) =>
`${source.firstName} ${source.middleName} ${source.lastName}`,
getFirstAndLastName: (source) => `${source.firstName} ${source.lastName}`,
getFirstNameLength: (source) => source.firstName.length,
});
type tests = [
Expect<Equal<typeof selectors['getFullName'], (source: Source) => string>>,
Expect<
Equal<typeof selectors['getFirstAndLastName'], (source: Source) => string>
>,
Expect<
Equal<typeof selectors['getFirstNameLength'], (source: Source) => number>
>
];
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-04-17 [React Suspense] Lazy load component with React suspense
2019-04-17 [Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined
2019-04-17 [Angular CLI] Build application without remove dist folder for Docker Volume
2019-04-17 [Spring Boot] Introduce to Mockito
2016-04-17 [RxJS] Returning subscriptions from the subscribe function
2016-04-17 [RxJS] Creation operator: create()
2016-04-17 [RxJS] Creation operators: interval and timer