【问题标题】:Typescript - create type of all available css propertiesTypescript - 创建所有可用 css 属性的类型
【发布时间】:2021-02-15 08:07:26
【问题描述】:

我正在使用 React 和 Styled Components,并且我有一个函数可以接受 css 属性的字符串。我想输入它来验证该字符串是一个有效的 css 属性(颜色、背景颜色等)

function doSomething(cssPropertyName: SomeType) {
}

doSomething('border-color'); // No error
doSomething('non-existing-css-property'); // Should give an error

我怎样才能做到这一点?
谢谢!

【问题讨论】:

  • 参考:这可能吗? github.com/frenic/csstype(prop: keyof CSSProperties) => ... 虽然您需要提供 borderColor 而不是 border-color(例如)。
  • 是的!那是东西。我很擅长骆驼案。你想添加这个作为答案吗?谢谢!

标签: css reactjs typescript css-in-js


【解决方案1】:

With:https://github.com/frenic/csstype 我想你可以简单地使用:

function doSomething(prop: keyof CSSProperties) {
}

doSomething('borderColor'); // No error
doSomething('non-existing-css-property'); // Should give an error

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2020-06-16
    • 2021-12-20
    • 2016-10-03
    相关资源
    最近更新 更多