简体   繁体   English

如果你想要所有 css 属性作为 typescript 中的一个类型,你放什么?

[英]if you want all css properties as a type in typescript what do you put?

StyleProps is used for size and colour. StyleProps 用于大小和颜色。

I would prefer it was all styles.我宁愿全是 styles。

This is so I can hand down the styles directly into the specific part of the component.这样我就可以将 styles 直接传递到组件的特定部分。

what do I put in to mean the types of all CSS properties?我输入什么来表示所有 CSS 属性的类型?

how do I change the styles to be all styles?如何将 styles 全部更改为 styles?

interface StyleProps{
  color?: string;
  size?: string;
}

interface TextButtonProps {
  buttonStyle?: styleProps;
  click: () => void;
  text: string;
}

const TextButton = ({ buttonStyle, text, click }: TextButtonProps) => {

You can extend the built-in type CSSStyleDeclaration (ES6 lib typings):您可以扩展内置类型CSSStyleDeclaration (ES6 库类型):

interface StyleProps extends Partial<CSSStyleDeclaration> {
  color?: string; // ! not necessary
  size?: string;
}

interface TextButtonProps {
  buttonStyle?: StyleProps;
  click: () => void;
  text: string;
}

const TextButton = ({ buttonStyle, text, click }: TextButtonProps) => {

Partial is used because the built-in type's properties are all required.使用Partial是因为内置类型的属性都是必需的。

Playground 操场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript - 创建所有可用 css 属性的类型 - Typescript - create type of all available css properties 在 Typescript 中,如果事先不知道对象的属性,您如何键入它? - In Typescript, how do you type an object when it's properties are not know beforehand? 如何将自定义属性传递给TypeScript + StyledCompoments? - How do you pass custom properties to TypeScript + StyledCompoments? 在 Typescript 中,PoorMansUnknown = {} | 类型是什么? 空| 未定义,您将如何处理? - In Typescript, what is type PoorMansUnknown = {} | null | undefined and how would you deal with it? 如何在 TypeScript 的 catch 块中键入错误属性? - How do you type an error property in a catch block in TypeScript? 你如何在 React Native 中为 useRef 钩子设置 Typescript 类型? - How do you set the Typescript type for useRef hook in React Native? RTK查询会把所有数据都存起来吗? 如果是,为什么还需要将数据放入切片中? - Will RTK query put all data to store? if yes, why do you need to put the data in slice too? 如何正确使用 Typescript 中的 React.lazy() 来导入带有泛型类型参数的反应组件? - How do you correctly use React.lazy() in Typescript to import a react component with a generic type parameter? 你如何键入一个作为道具传递给组件的 useState 钩子,typescript + react - How do you type a useState hook that is being passed as a prop to a component, typescript+react 你如何从 React 组件中提取特定的 TypeScript prop 类型? - How do you extract a specific TypeScript prop type from a React component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM