确保字符串是带流的十六进制值 [英] Ensure string is a hex value with flow

查看:49
本文介绍了确保字符串是带流的十六进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保我得到的值是一个十六进制字符串.目前我说

I would like to ensure that a value I get is a hex string. Currently I say

type Color = string;
function foo(color: Color){}

但是我想说

type Color = '#' + stringOfLength-3-6-8;
function foo(color: Color){}

有没有一种方法可以表达这种约束?

Is there a way to express constraints like this in flow?

推荐答案

不,这不是类型系统可以表达的.您可以创建一个由字面量的并集(例如type Foo = 'foo' | 'bar' | ...)组成的类型,但是除非您可以枚举每个可能的字符串,否则您将无法完成所要查询的内容.

No, this is not something that the type system can express. You can create a type which is a union of literals (e.g. type Foo = 'foo' | 'bar' | ...) but unless you can enumerate every possible string, you can't accomplish what you're asking.

这篇关于确保字符串是带流的十六进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆