How to extend the CustomToken object type definition for antd-style?#
Solution#
By extending the type definition of the CustomToken interface for antd-style, you can add corresponding token type definitions to the useTheme hooks.
At the same time, by adding generics to the ThemeProvider object, you can constrain the input definition of customToken.
import { ThemeProvider, useTheme } from 'antd-style';
interface NewToken {
customBrandColor: string;
}
// By extending the type definition of the `CustomToken` interface for `antd-style`, you can add corresponding token type definitions to the `useTheme` hooks
declare module 'antd-style' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface CustomToken extends NewToken {}
}
const App = () => {
const token = useTheme();
return <div>{token.customBrandColor}</div>;
};
export default () => (
// By adding generics to the `ThemeProvider` object, you can constrain the input definition of the `customToken` interface
<ThemeProvider<NewToken> customToken={{ customBrandColor: '#c956df' }}>
<App />
</ThemeProvider>
);