TypeScript
React Native Styled System aims for type-safe use.
Please enter the path to the module where ThemedDict is default exported as an argument to the command.
generate-theme-type src/AppTheme.ts
More convenient use is possible with scripts in package.json.
{
/* ... */
"scripts": {
"theme:gen": "generate-theme-type src/AppTheme.ts"
}
}
- npm
- yarn
npm run theme:gen
yarn theme:gen
Theme File Import Rules
Keep your theme.ts file focused on tokens only.
- For parser-friendly
theme.ts, import only theme utilities you actually use:createTheme,defaultThemefrom@react-native-styled-system/utilcreateThemeColorsfrom@react-native-styled-system/util(if you generate semantic tokens)
- Do not import from
react-native,react, hooks, providers, or app runtime modules.theme.tsshould behave like a static token module. - Extra imports can break CLI theme type generation during bundling.
If theme.ts imports runtime modules (react-native, react, hooks, providers, app services),
generate-theme-type can fail and stop the CLI.
Keep theme.ts limited to pure theme utilities and token data.
// parser-friendly theme.ts
import { createTheme, defaultTheme, createThemeColors } from '@react-native-styled-system/util';
When changing the version of a package or installing a new one, you must always execute the type creation command.
We recommend using the postinstall script so that it can operate without problems in CI as follows.
{
"postinstall": "yarn theme:gen"
}
How it works
used internally in the package
File in the path node_modules/@react-native-styled-system/core/lib/typescript/src/@types/ThemedTypings.d.ts
Regenerate it yourself.
Then it is created as follows:
import type { ColorsValue, RadiiValue, SizesValue, SpaceValue } from './Token';
export interface ThemedTypings {
colors:
| (ColorsValue & {})
| 'white'
| 'black'
| 'transparent'
| 'gray50'
/* ... */
| 'blue900';
radii: (RadiiValue & {}) | (`${number}` & {}) | (`${any}px` & {}) | '1' | '2' | 'sm' | 'md';
sizes:
| (SizesValue & {})
| (`${number}` & {})
| (`${any}px` & {})
| '0'
/* ... */
| 'px'
| '0.5';
space:
| (SpaceValue & {})
| (`${number}` & {})
| (`${any}px` & {})
| '0'
/* ... */
| '0.5'
| '-0.5';
typography: 'h1';
}