【发布时间】:2021-03-27 12:56:19
【问题描述】:
emotion.d.ts
import '@emotion/react';
declare module '@emotion/react' {
export interface Theme {
colors: {
primaryColor: string;
accentColor: string;
};
}
}
App.tsx
import { Theme, ThemeProvider } from '@emotion/react';
const theme: Theme = {
colors: {
accentColor: 'hotpink',
primaryColor: 'aqua',
},
};
...
return (
<ThemeProvider theme={theme}>
...
Button.tsx
const StyledButton = styled.a`
${({ theme }) =>
`background-color: ${theme.colors.accentColor};`
}`;
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"sourceMap": true,
"typeRoots": ["src/@types"],
"jsx": "react",
"strict": true,
"strictNullChecks": true,
"baseUrl": "./",
"paths": {
"*": ["src/@types/*"]
}
},
"compileOnSave": false,
"include": ["src/**/*"]
}
src
|=>@types
|> emotion.d.ts
|=> components
|> App.tsx
“对象”类型上不存在属性“颜色”。
是我做错了什么还是我误读了文档?
现在我通过将主题传递给样式构造函数来手动添加主题:
type StyledProps = Pick<ButtonProps, 'bgColor' | 'fColor'> & { theme: Theme };
const StyledButton = styled.a<StyledProps>`
...
删除传递的类型也无济于事。
d.ts 文件似乎被正确拾取,因为我可以从 "@emtion/react" 和 import { Theme } from "@emotion/react" 导入正确的主题类型,并使用它在样式组件中键入 props.theme
Repro
【问题讨论】:
标签: typescript emotion