Sam*_* M. 1 javascript css colors typescript reactjs
我的组件接受一个overlay应该是有效 CSS 颜色的道具。
当我在样式对象中CTRL + Click的color属性上时,类型定义来自csstypenode_modules 中的文件夹。CSS 颜色属性的类型定义定义为Property.Color. 我将该类型导入到我的组件中并将其用作我的overlay道具类型,但是any当我尝试使用该组件时它最终会出现。
我的组件的类型定义:
import { Property } from "../node_modules/csstype/index";
export interface BlurredComponentProps {
overlay?: Property.Color;
}
Run Code Online (Sandbox Code Playgroud)
这是我使用组件时的样子:
所以我的问题是,如何正确输入一个应该只采用有效 CSS 颜色的道具,如果给出了非颜色值,则给出错误?
谢谢
这个很难在 TypeScript 的类型系统中编码。我相信一个成熟的解析器可以在速度和准确性方面做得更好。
无论如何,如果您真的想color从打字稿中对您的值进行一些类型检查,那么让我们从 w3c 颜色属性描述开始:
Values: <color value> | <color keyword> | currentColor | transparent | inherit
Run Code Online (Sandbox Code Playgroud)
为那些不需要解释和直接查看代码的人提供的操场链接。
嗯,color keyword,currentColor,transparent和inherit是非常简单的:
Values: <color value> | <color keyword> | currentColor | transparent | inherit
Run Code Online (Sandbox Code Playgroud)
棘手的部分是<color value>:
The color can be specified as
* a hexadecimal RGB value: #faf or #ffaaff
* a RGB value: rgb(255, 160, 255) or rgb(100%, 62.5%, 100%)
Each value is from 0 to 255, or from 0% to 100%.
* a RGBA value: rgba(255, 160, 255, 1) or rgba(100%, 62.5%, 100%, 1)
This variant includes an “alpha” component to allow
specification of the opacity of a color. Values are
in the range 0.0 (fully transparent) to 1.0 (fully opaque).
* a HSL value: hsl(0, 100%, 50%)
A triple (hue, saturation, lightness). hue is an
angle in degrees. saturation and lightness are
percentages (0-100%).
* a HSLA value: hsla(0, 100%, 50%, 1)
This variant includes an “alpha” component to allow
specification of the opacity of a color. Values are
in the range 0.0 (fully transparent) to 1.0 (fully opaque).
Run Code Online (Sandbox Code Playgroud)
hexadecimal RGB value 还是可以的:
type Color = ColorValue | ColorKeyword | 'currentColor' | 'transparent' | 'inherit'
type ColorKeyword =
| "black"
| "silver"
| "gray"
...
| "rebeccapurple"
Run Code Online (Sandbox Code Playgroud)
我们必须引入类型变量T。否则为“扁平”联合类型:
The color can be specified as
* a hexadecimal RGB value: #faf or #ffaaff
* a RGB value: rgb(255, 160, 255) or rgb(100%, 62.5%, 100%)
Each value is from 0 to 255, or from 0% to 100%.
* a RGBA value: rgba(255, 160, 255, 1) or rgba(100%, 62.5%, 100%, 1)
This variant includes an “alpha” component to allow
specification of the opacity of a color. Values are
in the range 0.0 (fully transparent) to 1.0 (fully opaque).
* a HSL value: hsl(0, 100%, 50%)
A triple (hue, saturation, lightness). hue is an
angle in degrees. saturation and lightness are
percentages (0-100%).
* a HSLA value: hsla(0, 100%, 50%, 1)
This variant includes an “alpha” component to allow
specification of the opacity of a color. Values are
in the range 0.0 (fully transparent) to 1.0 (fully opaque).
Run Code Online (Sandbox Code Playgroud)
将由16^3 + 16^6远远超出100000联合类型的打字稿限制的成分组成。
让我们介绍一些辅助类型来处理数字。我们必须检查百分比不大于100%并以%字符结尾。
type HexDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f'
type Hex3 = `${HexDigit}${HexDigit}${HexDigit}`
type RGBColor<T extends string> =
Lowercase<T> extends `#${Hex3}`
? T
: Lowercase<T> extends `#${Hex3}${infer Rest}`
? Rest extends Hex3
? T
: never
: never
Run Code Online (Sandbox Code Playgroud)
此外,颜色值必须是整数且不大于255:
type RGBColor = `#${Hex3}` | `#${Hex3}${Hex3}`
Run Code Online (Sandbox Code Playgroud)
因此,任何颜色值都可以编码为[0..255]范围内的整数或百分比:
type DecDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
type Digits0to4 = '0' | '1' | '2' | '3' | '4'
type OnlyDecDigits<T extends string> =
T extends `${DecDigit}${infer Rest}`
? Rest extends ''
? 1
: OnlyDecDigits<Rest>
: never
type IsDecNumber<T extends string> =
T extends `${infer Integer}.${infer Fractional}`
? Integer extends ''
? OnlyDecDigits<Fractional>
: Fractional extends ''
? OnlyDecDigits<Integer>
: OnlyDecDigits<Integer> & OnlyDecDigits<Fractional>
: OnlyDecDigits<T>
type IntegerPart<T extends string> =
T extends `${infer I}.${infer F}`
? I
: T
type IsInteger<T extends string> =
1 extends IsDecNumber<T>
? T extends IntegerPart<T>
? 1
: never
: never
type Less100<T extends string> =
IsDecNumber<T> extends 1
? IntegerPart<T> extends `${DecDigit}` | `${DecDigit}${DecDigit}` | '100'
? 1
: never
: never
type IsPercent<T extends string> =
'0' extends T
? 1
: T extends `${infer P}%`
? Less100<P>
: never
Run Code Online (Sandbox Code Playgroud)
添加实用程序Trim类型以修剪两端的额外空间:
type Color255<T extends string> =
1 extends IsInteger<T>
? T extends `${DecDigit}`
| `${DecDigit}${DecDigit}`
| `1${DecDigit}${DecDigit}`
| `2${Digits0to4}${DecDigit}`
| `25${Digits0to4 | '5'}`
? 1
: never
: never
Run Code Online (Sandbox Code Playgroud)
这足够了rgb:
type IsColorValue<T extends string> = IsPercent<T> | Color255<T>
Run Code Online (Sandbox Code Playgroud)
对于rgba/hsla我们需要不透明度。在这里,我们只要求输入任何有效数字或百分比:
type WhiteSpace = ' '
type Trim<T> = T extends `${WhiteSpace}${infer U}`
? Trim<U>
: T extends `${infer U}${WhiteSpace}`
? Trim<U>
: T;
Run Code Online (Sandbox Code Playgroud)
现在我们可以检查rgba值:
type RGB<T extends string> =
T extends `rgb(${infer R},${infer G},${infer B})`
? '111' extends `${IsColorValue<Trim<R>>}${IsColorValue<Trim<G>>}${IsColorValue<Trim<B>>}`
? T
: never
: never
Run Code Online (Sandbox Code Playgroud)
为hsl/添加度数检查器hsla:
type Opacity<T extends string> = IsDecNumber<T> | IsPercent<T>
Run Code Online (Sandbox Code Playgroud)
最后我们可以涵盖最后一种情况:
type RGBA<T extends string> =
T extends `rgba(${infer R},${infer G},${infer B},${infer O})`
? '1111' extends `${IsColorValue<Trim<R>>}${IsColorValue<Trim<G>>}${IsColorValue<Trim<B>>}${Opacity<Trim<O>>}`
? T
: never
: never
Run Code Online (Sandbox Code Playgroud)
所以我们的最终类型看起来像这样:
type Degree<T extends string> =
1 extends IsInteger<T>
? T extends `${DecDigit}`
| `${DecDigit}${DecDigit}`
| `${'1' | '2'}${DecDigit}${DecDigit}`
| `3${Digits0to4 | '5'}${DecDigit}`
| '360'
? 1
: never
: never
Run Code Online (Sandbox Code Playgroud)